0

I'm getting this error and I honestly have no idea what to do to fix it, I've tried everything I can think of.

The code is to check the player's boot slot to see if he has diamond boots on, and if they do, to set it as air as it is a banned item.

The error:

Too big to paste in here so here it is: http://pastebin.com/zhzc3Hut

The code:

    @EventHandler
public void onInventoryClickBoots(InventoryClickEvent event) {
    Player player = (Player) event.getWhoClicked();

    if(player.getInventory().getBoots().getType().equals(Material.DIAMOND_BOOTS)){
        player.getInventory().setBoots(new ItemStack(Material.AIR));
    }   
    else {
    }    
}

I'm also getting this repeated error for the helmet, chestplate and leggings slot too.

Thanks in advance for anyone that is able to help me out! An explanation of what's going on would also be greatly appreciated!

Justinw
  • 631
  • 10
  • 18
  • from the stacktrace: InventoryScanner.onInventoryClickBoots(InventoryScanner.java:93). Which is line 93 in the code above? – Aris2World Aug 01 '16 at 12:27
  • if(player.getInventory().getBoots().getType().equals(Material.DIAMOND_BOOTS)){ – CameronOfoluwa Aug 01 '16 at 12:32
  • ok. As stacktrace tells us we have a NullPointerException. The not initialized reference could be any on the line 93. player? getInventory()? getBoots()? getType()? I suggest to put some not null check before access to a reference. In case of null emit a log or a message to locate which is the first null reference you run into. – Aris2World Aug 01 '16 at 12:40

1 Answers1

2

The problem you're probably facing is that the getBoots ItemStack is actually null.

To fix that issue, we should first check if the boots are null before checking it's type.

ItemStack boots = player.getInventory().getBoots();
if (boots != null) {
    if (boots.getType().equals(Material.DIAMOND_BOOTS) {
       player.getInventory().setBoots(null);
    }
}

@Edit

As reminded by The_Lone_Devil:

(...)This is because an empty slot in an inventory is a null ItemStack, so if they have nothing in the boots slot, the ItemStack that is returned is null.

LeoColman
  • 6,950
  • 7
  • 34
  • 63
  • 1
    Would like to add that this is because an empty slot in an inventory is a null ItemStack, so if they have nothing in the boots slot, the ItemStack that is returned is null. – Justinw Aug 01 '16 at 15:48