Solved NPE when checking if a player is null?

Discussion in 'Plugin Development' started by jojodmo, Mar 6, 2014.

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

    jojodmo

    I'm getting a NPE when I try to check if a player .equals null...

    Here's the error:

    Code:java
    1. [19:58:02 ERROR]: Error occurred while disabling Wizards v0.0.1 (Is it up to date?)
    2. > at net.podzol.jojodmo.wizards.Common.getUUID(Common.java:10) ~[?:?]
    3. > at net.podzol.jojodmo.wizards.Common.getUUID(Common.java:23) ~[?:?]
    4. > at net.podzol.jojodmo.wizards.Main.onDisable(Main.java:234) ~[?:?]


    and here's line 10 of Common.java:

    Code:java
    1. if(!player.equals(null)){


    it's in this method:

    Code:java
    1. public static String getUUID(Player player){
    2. if(!player.equals(null)){
    3. return player.getUniqueId().toString();
    4. }
    5. return "null";
    6. }


    What I'm trying to do is, I would like to get the UUID of a player, using the player's name, so for example, if "jojodmo" is put in (NOT CraftPlayer=jojodmo), I would like it to return my UUID... It get's UUID's fine, just when I try to get the UUID of a string, it doesn't work... I check if the player has played before here:

    Code:java
    1. public static String getUUID(String player){
    2. if(Bukkit.getServer().getOfflinePlayer(player).hasPlayedBefore()){


    Any ideas? The thing that has me stumped is why I'm getting a NPE when I'm checking if a player.equals(null);
     
  2. Offline

    Mmarz11

    jojodmo2010 Change it to:
    if(player != null)){

    If you try to call a method on a variable that is null it will throw a NPE. So checking if the variable is null by using a method is not a good idea.
     
  3. Offline

    jojodmo

    Wow... That would explain a lot... Thanks :)
     
  4. Offline

    Alshain01

    To further Mmarz11 explanation. null is a literal so you can use standard operators. You use .equals() on Objects and operators on literals and primitives.
     
Thread Status:
Not open for further replies.

Share This Page