ItemStack .getType() returns NPE ?

Discussion in 'Plugin Development' started by ThunderWaffeMC, Jan 13, 2015.

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


    For some strange reason that I don't know, item.getType() is returning a NPE error (even though player.sendMessage(item.getType()) returns STAINED_GLASS_PANE).


    2. @EventHandler
    3. public void inventoryMove(PlayerItemHeldEvent event) {
    4. ItemStack item = event.getPlayer().getInventory().getItem(event.getNewSlot());
    5. item.getType(); //this line will produce a NPE error
    6. if(item.getType() != null && item.getType() == Material.STAINED_GLASS_PANE) { //check if the item held was stained glass pane - error is here
    7. event.setCancelled(true);
    8. }
    9. }

    When using 'item' instead of 'item.getType()' it doesn't produce an error, so I'm guessing the error is because of .getType();. And for a note: .getTypeId(); produces the same error.

    Thanks for your help.
  2. Offline


    No, it's because item is null.
    ThunderWaffeMC likes this.
  3. Offline


    Ahh, righto. The event.setCancelled() moves the slot back and it calls the event again, hence the air slot which is null (.getType() wouldn't return null, it would return AIR).

Thread Status:
Not open for further replies.

Share This Page