Solved NullPointerException

Discussion in 'Plugin Development' started by Tencu, Feb 6, 2018.

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

    Tencu

  2. Offline

    timtower Administrator Administrator Moderator

    @Tencu Please post the full class.
     
  3. Offline

    Tencu

  4. Offline

    timtower Administrator Administrator Moderator

    @Tencu I think that you aneed to use ItemStack#hasItemMeta and ItemMeta#hasDisplayName
     
  5. Offline

    Tencu

    @timtower What do you mean by that? what would you replace it with.
     
  6. Offline

    Machine Maker

    Couple mistakes I noticed.

    1. Don't use player#getItemInHand(); that method has been deprecated since minecraft introduced off-hands. Use player#getItemInMainHand() or player#getItemInOffHand() for each hands.​

    2. To add on to timtower's suggestion, the item that the event is called from might not have an ItemMeta or a DisplayName in which case, those would be null causing your error. Before line 55, check to make sure the item has a meta with item#hasItemMeta() and if that passes, item#getItemMeta()#hasDisplayName().​
     
  7. Offline

    Tencu

    So instead of:
    player.getItemInHand()

    You would use:
    player.getItemInMainHand() ? or what, because my plugin works. it does what it's supposed to. but when you have another item / no item in your hand on right or left click it spams that error in console. So i already know that getItemInHand passes the DisplayName, so i can compare it to a string to see if it should execute.

    And btw getItemInMainHand() is not a thing, atleast to my knowledge / my IDE.
     
    Last edited: Feb 7, 2018
  8. @Tencu It's Inventory#getItemInMainHand
    They're also suggesting hasItemMeta and hasDisplayName since right now as you even said no item or no item with a display name will give errors. Add these checks to make sure there is actually a display name.
     
  9. Offline

    Tencu

    I have tried so many things now, i can't understand what the problem is.

    My current code: https://hastebin.com/evijesenuk.cs
    My current error when i use getItemInMainHand: https://hastebin.com/ucizizekoq.vbs
    (I most likely cant use this as i'm running the plugin on a server where offHand isnt introduced.)
    My current error when i use getItemInHand: https://hastebin.com/nebotodice.vbs

    I have tried everything you guys have suggested, Í simply have no earthly clue why it does as it does.

    @timtower @X1machinemaker1X @bwfcwalshy Any help would be so much appreciated because i have no clue why it's doing as it is.
     
    Last edited: Feb 8, 2018
  10. Offline

    Machine Maker

    Your current code will give errors when you interact with an empty hand. The material.Air itemstack doesn't have any ItemMeta, so before you check if the display name is null, you first have to check if the ItemMeta is null.
     
  11. Offline

    MrGriefer_

    Why not first check if the item in hand isn't null, then check if it has an item meta and has a display name.
    PHP:
        @EventHandler
        
    public void onPlayerInteractEvent(PlayerInteractEvent event) {
            
    Player p event.getPlayer();
            
    ItemStack is p.getItemInMainHand();
           
            if (
    is != null
                
    && is.hasItemMeta()
                && 
    is.getItemMeta().hasDisplayName()
                && 
    is.getItemMeta().getDisplayName().equals(Name)) {
               
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page