I have been constantly been getting a NullPointerException, no matter what code I use. I haven't coded in Java for a couple months, so I am a bit rusty, can anyone help me out? Code: for (ItemStack items : player.getInventory().getContents()) { if (items != null) if (items.hasItemMeta() && items.getType() == Material.WRITTEN_BOOK) if (((BookMeta) items.getItemMeta()).getTitle().equals(ChatColor.BLUE + "test")) items.setTypeId(0); }
no, just the items Why does items.equals(null) give an npe when I'm checking to see if its null? And whats the difference between using == and .equals() ? one seems to work sometimes and the other doesnt...? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
You can use .equals() on a null since a null object doesn't have any specific functions. Add some debug such as Code: if (player == null) { getLogger().info("Player is null"); } if (player.getInventory() == null) { getLogger().info("Inventory is null"); } to find the root of the NPE
I already did debugging. For some reason... it just stopped giving npes.... I did not change any code or order.... but now my problem is removing the item, item.setType() to AIR or 0 won't work..
Player's can't carry an air block, if I recall correctly you might be able to do player.getInventory().remove(items)
Yea, just found that method in the JavaDocs... I remember setting items to air a long time ago.. I really need to keep up with Java, been away soo long. well thanks for helping!