Solved java.lang.NullPointerException Error Bug

Discussion in 'Plugin Development' started by DomThePotato, Jan 11, 2015.

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

    DomThePotato

    So I am making a KitPvP plugin. I recently added a feature where there was a 'Kit Selector'. The Kit Selector works absolutely fine but the PlayerInteractEvent Listener in the class is causing a bit of console spam. And it's getting quite annoying.

    Every time someone interacts with a block I get this error http://pastebin.com/T84PS09A. On line 178 is the if statement for the Interact Listener 'if(is.getType().equals(Material.COMPASS)){'.

    Here is my whole event listener:

    Code:
    @EventHandler
            public void on(PlayerInteractEvent e){
                    ItemStack is = e.getItem();
         
                    if(is.getType().equals(Material.COMPASS)){
                                    openGUI(e.getPlayer());
                    }else{
                            return;
                    }
                 
            }
    That is all. If there is any more information you need please ask. Thank you for taking your time to read this. :D.
     
    Last edited by a moderator: Jan 11, 2015
  2. Offline

    BlazingBroGamer

    @DomThePotato
    Check if the e.getItem is null, then move onto the next code
     
  3. Offline

    DomThePotato

    @BlazingBroGamer
    So like:

    Code:
    if(is.getType().equals(null){
    return;
    }
    //Then check for compass                 
     
  4. Offline

    BlazingBroGamer

    @DomThePotato
    No, check if is == null. Thats how I do it. (Your method MIGHT work, I haven't tested it before)
     
  5. Offline

    mythbusterma

    @BlazingBroGamer

    No, it won't ever work, unless you consider throwing more NPE's "working". If the value were to be null at that point it would be the semantic equivalent of doing null.equals(null), which would be invoking a method on null, causing a NullPointerException.
     
  6. Offline

    DomThePotato

    Ok, sorry to be a pain but what do I do?
     
  7. Offline

    teej107

    @DomThePotato The event getItem() method won't always return an item. It will return null if no item was clicked.
    From reading the posts above, what do you think you should do?
     
  8. Offline

    DomThePotato

    @teej107
    if(!is.getType().equals(MATERIAL.Compass)||is.getType().equals(null){
    Return;
    } else {
    openGUI(e.getPlayer);
    }

    Sorry doing this on phone.
     
  9. Offline

    mythbusterma

    @DomThePotato

    If the item exists, it will never have a null Material.

    Also you're still trying to do a null check by invoking Object#equals(..) on null, which will generate a NullPointerException in and of itself.
     
  10. Offline

    1Rogue

    You don't compare null using .equals(), it's not an instantible type. You compare using ==
     
  11. Offline

    DomThePotato

    Ok, So I've done this

    Code:
        @EventHandler
        public void on(PlayerInteractEvent e){
           
            ItemStack is = e.getItem();
    
            if(!is.getType().equals(Material.COMPASS)||is.getType()==null){
            return;   
            }else if(is.getType().equals(Material.COMPASS)){
                openGUI(e.getPlayer());
                 return;
            }
    But I'm still getting the same error. I feel like I am missing some thing you're all trying to tell me.
     
  12. Offline

    unrealdesign

    If you read what the people who actually know what they were talking about said, you would understand that he told you to check
    Code:
    is == null
    and not what the person on their phone gave you as code.
     
  13. Offline

    _Cookie_

    Code:
    if(is == null){
    return;
    }
    Simple :)
     
  14. Offline

    DomThePotato

    Hi, sorry I took so long for me to get round to replying but I am still getting the same error.

    Here is my Event Handler:
    Code:
        @EventHandler
        public void on(PlayerInteractEvent e){
           
            ItemStack is = e.getItem();
            
            if (!(is.getType()==(Material.COMPASS))|| is == null){
                return;
            }else{
                openGUI(e.getPlayer());
                return;
            }
        }
    And I still get the same error.

    @unrealdesign Not intending to be rude but the 'person on their phone' happened to be me.

    Thanks for all the help you are giving me.

     
  15. Offline

    1Rogue

    You need to check for null before attempting to dereference a variable, not after. Re:

    Code:java
    1. if (!(is.getType()==(Material.COMPASS))|| is == null){
     
  16. Offline

    DomThePotato

    @1Rogue so like

    Code:
    if(is==null || //code){
    //Do this
    
    }
    ?
     
  17. Offline

    1Rogue

    Correct. The logical operators || and && are optimized in that they return as soon as possible. If the call "is == null" returns true, then it won't execute the second part that has "is.getType()", because it's an OR statement and only one needs to be true.
     
  18. Offline

    DomThePotato

    Thanks for all the help you all gave me! Very much appreciated! :D
     
Thread Status:
Not open for further replies.

Share This Page