Solved ENUM - Name is null

Discussion in 'Spigot Plugin Development' started by zThana, May 23, 2016.

Thread Status:
Not open for further replies.
  1. Heey,

    This got my attention:
    Code (Java):
    23.05 01:28:04 [Server] INFO Caused by: java.lang.NullPointerException: Name is null
    23.05 01:28:04 [Server] INFO at java.lang.Enum.valueOf(Enum.java:236) ~[?:1.8.0_74]
    23.05 01:28:04 [Server] INFO at com.pr0totype2.legacypve.enums.Races.valueOf(Races.java:1) ~[?:?]
    23.05 01:28:04 [Server] INFO at com.pr0totype2.legacypve.handlers.RaceHandler.getRace(RaceHandler.java:401) ~[?:?]
    23.05 01:28:04 [Server] INFO at com.pr0totype2.legacypve.handlers.RaceHandler.onInventoryClick(RaceHandler.java:103) ~[?:?]
    I noticed it was inside this, the "if" statement:
    Code (Java):
        public String getRace(Player player) {
            if (Races.valueOf(plugin.playersData.getString("players." + player.getUniqueId() + ".race")) != null) {
                return plugin.playersData.getString("players." + player.getUniqueId() + ".race");
            } else {
                return "NONE";
            }
        }
    No clue what "Name is null" is meant to be.
    Is it that plugin.playersData.getString("players." + player.getUniqueId() + ".race" isn't a value of the Races Enum?

    I have no idea, could someone please help me?
    - Pr0totype2
     
  2. Please show us your enum class.
     
  3. Your plugin.playersData.getString("players." + player.getUniqueId() + ".race") code is indeed returning null, you should be storing that as a string first and then checking if its not null and Races.valueOf(value) != null:
    Code (Java):
    public String getRace(Player player) {
        String race = plugin.playersData.getString("players." + player.getUniqueId() + ".race");
        if (race != null && Races.valueOf(race) != null) {
            return race;
        } else {
            return "NONE";
        }
    }
     
  4. Oh like that!
    I'll try it out, thanks ^-^
     
  5. Check if this is null. Also, valueOf throws IllegalArgumentException; it doesn't return null.
     
    • Informative Informative x 1
  6. @PickNChew is correct, your code should actually be:
    Code (Java):
    public String getRace(Player player) {
        String race = plugin.playersData.getString("players." + player.getUniqueId() + ".race");
        if (race != null) {
            try {
                return Races.valueOf(race);
            } catch (Exception ex) { }
        }
        return "NONE";
    }
     
    • Agree Agree x 2
    • Useful Useful x 1
  7. I'd recommend using FileConfiguration#getString( String path, String defaultValue )

    If it's null, it will return the default value. No need for null checks here.
     
  8. Shouldn't you be catching IllegalArgumentException?
     
  9. Code (Java):
        public String getRace(Player player) {
            String race = plugin.playersData.getString("players." + player.getUniqueId() + ".race");
            if (race != null) {
                try {
                    return Races.valueOf(race).getRole();
                } catch (IllegalArgumentException ex) {
                }
            }
           
            return "NONE";
        }
     
  10. Return type error.. Using an IDE would help?
     
  11. Idk what you meant by that, I'm using Eclipse.
    Code (Java):
        public String getRace(Player player) {
            String race = plugin.playersData.getString("players." + player.getUniqueId() + ".race");
            if (race != null) {
                try {
                    return Races.valueOf(race).getRole();
                } catch (IllegalArgumentException ex) {
                    ex.printStackTrace();
                }
            }
           
            return "NONE";
        }
     
  12. MiniDigger

    Supporter

    where did you spot that? return type is String, he returns the string "NONE" or Races#getRole() (I am assuming that that will return a string)
     
    • Agree Agree x 2
  13. Never saw the getROle call, mybad
     
    • Friendly Friendly x 1
  14. Sure but it doesn't really matter because the only other exception that could be thrown in this case I s if that Enum or getRace() method didn't exist, but it wouldn't necessarily compile with yoyr average IDE.

    He should have made a static method that removes any risk of an exception anyway.
     
Thread Status:
Not open for further replies.