Player Death Event NPE

Discussion in 'Plugin Development' started by 1SmallVille1, Mar 8, 2013.

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

    1SmallVille1

    So I was wondering what was throwing the NPE in my class. This is what my code looks like:

    Code:
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent evt) {
            if (!(chosen.equals(null))){
                Bukkit.getServer().broadcastMessage("someone died");
            }
        }
    
     
  2. Offline

    ZeusAllMighty11

    You're checking for a null value on what I assume would be a string?
     
  3. Offline

    1SmallVille1

    yes, a player name
     
  4. Offline

    ZeusAllMighty11

    I'm confused what you're even doing
     
  5. Offline

    1SmallVille1

    Well, in another class, I define the "chosen" variable with a command, then here, I check if it's null (whether the command has been excecuted) if it's not, then it broadcasts the message
     
  6. Offline

    GodzOfMadness

    1SmallVille1 We really can't help you unless you show more of your code
     
  7. Offline

    1SmallVille1

    All right but the only thing left is me defining variables:
    Code:
    public class PvPListener implements Listener {
     
    public static String chosen = Commands.chosen;
       
    @EventHandler
        public void onPlayerDeath(PlayerDeathEvent evt) {
            if (!(chosen.equals(null))){
                Bukkit.getServer().broadcastMessage("someone died");
            }
        }
    }
     
  8. Offline

    Technius

    If chosen is null, then there will be an NPE because you will be trying to invoke a method on a null object. It should be
    Code:
    if(chosen == null)
    
     
  9. Offline

    GodzOfMadness

    1SmallVille1 You should try and initialize the Commands class by doing it like this
    public static Commands c;
    public PvPListener(Commands c){
    this.c = c;
    }
    then do public static String chosen = c.chosen;
     
  10. Offline

    1SmallVille1

    but then shouldn't this work also? because this is what I originally had but it threw the same error
    Code:
    if (chosen != null)
     
  11. If (chosen != null) can never throw a NullPointerException. Be sure if it is really the same by comparing the stack trace and than get the exact line from the stack trace and investigate that, or post it here to get more help.
     
  12. Offline

    Technius

    chosen != null is seeing if the pointer object is null. On the other hand, chosen.equals is a method and will throw a NPE if chosen is null.
     
Thread Status:
Not open for further replies.

Share This Page