Here's my code Code: @EventHandler public void onInteract(PlayerInteractEvent event) { // Check for right click if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) { // Check for item if (event.hasItem()) { // Check for radio if (Radio.isRadio(event.getItem())) { // Get item ItemStack stack = event.getItem(); boolean state; // Check current state if (stack.getType() == Material.REDSTONE_LAMP_ON) { // Set it to off stack.setType(Material.REDSTONE_LAMP_OFF); state = false; } else { // Set it to on stack.setType(Material.REDSTONE_LAMP_ON); state = true; } // Now manipulate the meta ItemMeta meta = stack.getItemMeta(); List<String> lore = meta.getLore(); lore.set(3, Radio.getStateString(state)); meta.setLore(lore); stack.setItemMeta(meta); } } } } And I get an NPE on the like with List<String> lore = meta.getLore(); What is going on here? The method Radio.isRadio() checks within that the item has a meta and lore, so it should be working. Also, the NPE is thrown when this listener is called, but the item im holding has a lore already!
@Pocketkid2 The ItemMeta will normally not be null, but the Lore can, check if the meta #hasLore before trying to use it, also, it's good practice to also check if the item #hasItemMeta
Well then I am not sure because A) I know that method checks because I just checked and B) The item this is being called on does in fact have a meta (It was set explicitly earlier, and also, why would it not have a meta if it has a displayname and lore?)
Code: public static boolean isRadio(ItemStack stack) { if (stack.getType() == Material.REDSTONE_LAMP_ON || stack.getType() == Material.REDSTONE_LAMP_OFF) { if (stack.hasItemMeta()) { ItemMeta meta = stack.getItemMeta(); if (meta.hasDisplayName() && meta.getDisplayName().equalsIgnoreCase("Radio")) { if (meta.hasLore()) { List<String> lores = meta.getLore(); if (!Radio.tier.matcher(lores.get(0)).matches()) { return false; } if (!Radio.radius.matcher(lores.get(1)).matches()) { return false; } if (!Radio.frequency.matcher(lores.get(2)).matches()) { return false; } if (!Radio.state.matcher(lores.get(3)).matches()) { return false; } return true; } } } } return false; }
It's a regex class that I'm using to parse the lore format. But it doesn't matter here, and I've tested it so I know it's working.
Is the NPE on this line specifically? Code: List<String> lore = meta.getLore(); Because if the Lore is null, an NPE won't be thrown here specifically, but if the meta is null, it will. So if it's here I'm guessing it's a question of the meta its self being null I think i maybe figured it out.. maybe EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Up at the first code snippet on line 8 I'm calling Radio.isRadio(). The only return true statement in that function is after some IF statements that check if the stack A) has a item meta B) Has a display name AND C) has a lore. Now back to the first one. All I'm doing after Radio.isRadio() is pulling the itemstack, updating its information, and getting the meta so that I can edit it further. Something funky is up here.
Oh just a word for the future (this isnt the problem), when you're looking through a list of something, make sure the size of the list isnt smaller than the index of what you're looking for, just in case you get an ArrayIndexOutOfBounds exception sometime