Solved A null on my enum

Discussion in 'Plugin Development' started by hanahouhanah, Aug 30, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    hanahouhanah

    So I have an enumeration and a method to go through that enumeration and find the enum.name() based off its first given value.
    However, it only works if I hard-write the string or store that hard-written string in a variable. I can't do this:
    player.getItemInHand().getItemMeta().getDisplayName()

    In code, it looks like this:
    Code:
    enumListNames elnWorking = enumListNames("Cool Sword"); //this actually returns the enum name
    enumListNames elnNotWorking = enumListNames(player.getItemInHand().getItemMeta().getDisplayName()) //Returns "null" even if the name is "Cool Sword" on that item (checked through debugging)

    This is my method for finding that enum given one of its values.

    Code:
    public static enumListNames findItemOffName(String name)
        {
            for(enumListNames cim : values())
            {
                if(cim.getItemName() == name)
                {
                    return cim;
                }
            }
            return null;
        }
    -

    I've tried the following:
    Storing player.getItemInHand().getItemMeta().getDisplayName() into a String variable first and using that variable in the enumListNames parameter, checking the actual name value of the itemInHand, tried other items which existed in the enumeration, and a lot of other debugging like checking every value up to the error (null value).
    Any ideas on how to fix this sort of thing?
     
  2. @hanahouhanah
    Print in your console the item's display name and there's your awnser. Also, please follow java naming conventions: Make "enumListNames" "EnumListNames"
     
  3. Online

    timtower Administrator Administrator Moderator

  4. Offline

    mine-care

    @hanahouhanah

    Java is the problem! At least the basic knowledge is required before you begin with Plugin Development,
    A few notes to improve your code and hopefully solve your problem:
    1. Follow java naming convention! Classes follow CamelCapitalisation!
    2. Dont compare Objects with == unless they are singleton's (which String isn't)
    3. Dont forget, there is the Enum#valueOf(String param) method, you dont have to create one yourself.

    Also make sure the item has itemmeta and a displayname before getting the name.
     
    hanahouhanah likes this.
  5. Offline

    hanahouhanah

    WHOOOPS
    Haha, that mistake flipped right over my head. I'll respond to everything here:
    #1) I actually do follow camel case. Least, in terms of C# it's how I've learned what camel case was (and it's how I do it: yourName, myName, etc.) I looked up what you were talking about and it seems that naming convention is called "Pascal case." Then again, it doesn't really matter which naming convention one follows so long as it's logical and readable.
    #2) That was my mistake. I read this line and thought to myself, "why would you say that, I don't think I've done that anywhere in my code because I should've fixed that mistake if I've made it."
    The problem probably happened in the first place because I was trying a varying different methods of grabbing the enum's name by using one of its values first so some of the code was reused with a few things changed. I guess that part was something I didn't change. Thanks for pointing this out because I would've never thought to look at that again
    #3) Doesn't "valueOf" just grab the enum constant, where I'd have to actually know the constant's name first? If so then that's not what I needed which is why I made my own method to search through the list of the enum's values so it can be matched with the constant. If that makes any sense.
     
  6. Offline

    mine-care

    @hanahouhanah

    1. I see the confusion there, they are vital when you are reading code
    3. Well from what I see you check if the string equals the current value of the loop, if so you return the enum,
    So it is exacly the same but only the one you use will not work properly (object comparason with ==)
    So yeah if you use valueOf(string.toUpperCase()); that will do (keeping in mind that the enum values by convention are uppercase)
     
  7. Offline

    hanahouhanah

    @mine-care
    I see what you're trying to say (I think, commenting this at 2am so my brain is just starting to go loopy). My objective was different than what you think, though.
    Say I had this enum:
    Code:
    public enum someEnumThing{
    
    firstItem("Something In It");
    
    //and the constructor and all that jazz
    }
    Say I had a huge list of constants in that enumeration class, and I had to access the constant (where the possible constant is dynamically changed by the user/player) but I only had the value that's in the constant. In this case, the string.
    For that I'd have to get access to the constant using the method I kinda just farted out of my brain, because it's easier than doing an individual check or looping it in my opinion. Unless what you're saying is I could just do valueOf("Something In It") and it'd return the constant "firstItem" in which case I had no idea.
    -
    But yea. My problem was fixed when you pointed that I didn't change "==." I changed it and now it works just fine. :)
     
Thread Status:
Not open for further replies.

Share This Page