Solved NPE...

Discussion in 'Spigot Plugin Development' started by VojtaCRV, Jul 27, 2018.

  1. Hello.
    And again, a NPE i cant figure out... but now i really have no clue..
    Code (Java):
        public static String getNamee(ItemStack itemstack) {
            return itemstack.getItemMeta().getDisplayName(); //NPE LINE
        }
    Whats up with it?
     
  2. itemstack could be null
    Same counts for ItemMeta
    Therefor will displayname also be null, in most cases

    Tried debugging? And how are you calling this method?
     
    • Agree Agree x 2
    • Like Like x 1
  3. The display name cannot be null in this case. If the display name is null, the method does not throw an exception - it just returns null. Since the method throws an exception, either the ItemStack is null, or the ItemMeta returned from the non-null ItemStack is null.

    This is a subtle detail, but if the ItemStack is null, you can't really say that the ItemMeta is null, because there is no ItemStack for an ItemMeta field (or a getItemMeta() method) to be referenced. That is, nulls do not "propagate". The chain is interrupted at the occurrence of a null value because "nothing" is there to call methods upon.

    While this might seem like nitpicking, it's important to note that misinformation can be a huge source of confusion :)
     
    • Informative Informative x 2
  4. The ItemStack can actually be null. If the ItemMeta doesn't exist it will also return null (or if it is empty, can't remember). If there is no displayName set to the meta then it will return null.


    This will most likely help you out
    Code (Java):

    public static String getName(ItemStack itemstack) {
    if (itemstack == null){
    Bukkit.broadcastMessage("The itemstack is null");
    return;
    }

    if (!itemstack.hasItemMeta()) {
    Bukkit.broadcastMessage("The item " + itemstack.getType() + " has no ItemMeta");
    return;
    }

    ItemMeta meta = itemstack.getItemMeta();

    if (!meta.hasDisplayName()) {
    Bukkit.broadcastMessage("The item " + itemstack.getType() + " has no displayName");
    return;
    }


    String disName = meta.getDisplayName();

            return disName; // Should work fine
        }
     
     
    • Winner Winner x 1
  5. You must have misread - I never said that the ItemStack can't be null.

    I said that the display name can't be null in this case. If the display name is null, the method returns null. But it throws an NPE in this case. This means that either the ItemStack is null, or the ItemMeta is null, in this case. But if the ItemStack is null, there is nothing for an ItemMeta reference to exist in. It makes no sense to say that "if the ItemStack is null, the ItemMeta is also null", because nulls do no "propagate" like that.

    That said, if the ItemMeta reference in the ItemStack is null, getItemMeta() will call Bukkit.getItemFactory().getItemMeta(getType0()). I'm not sure if that call can ever return null, but I assume it won't and just always create a fresh ItemMeta for the ItemStack.
     
    • Informative Informative x 1
  6. ItemMeta will never return null unless you're specifying air as the itemstack. Otherwise, an empty meta will be created.
     
    • Informative Informative x 2
  7. Every one else has summed this up quite nicely.
    Since this throws an exception (which should print a stack trace), it shouldn't be too difficult to figure out where the null argument is being generated. The stack trace should show the methods that were called in order to get to the point that was throwing the exception, so what I'd generally do is basically what darklazerog said (simply print debug messages if an object is null), but also do so in each method that comes up in the stack trace prior to the exception. This way you can track when the data becomes null, and focus the investigation on that point.
     
    • Like Like x 1
  8. shorter: check if item is null, if item has itemmeta, if itemmeta has displayname
     
    • Winner Winner x 1
  9. It works perfectly now! Thank so much! :)

    Thats good to know. :)