craftInventory.getContents(); returns: ItemMeta = null

Discussion in 'Plugin Development' started by DutchJellyV2, Feb 13, 2018.

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

    DutchJellyV2

    Hi helpful bukkit people,

    I'm stuck with a problem. The ItemStack.getItemMeta() is returning null. Some code:

    Code:
    CraftingInventory craftInv = e.getInventory();
            if(craftInv.getContents() != null){
                ItemStack[] content = craftInv.getContents();
    Code:
    System.out.println("recipe: " + recipe[i-1].getType().name() + " content:" + content[i].getType().name() + " recipe:" + recipe[i-1].getEnchantments().size() + " content:" + content[i].getEnchantments().size());
                            if(content[i].getItemMeta() == null){
                                System.out.print("(item meta == null) = true");
                            }
    For some reason the Console output was:
    recipe: ENCHANTED_BOOK content:ENCHANTED_BOOK recipe:1 content:0
    Also:
    (item meta == null) = true
    Anyone that has any ideas on what I did wrong would be a hero to me!
    Thanks in advance.

    -DutchJelly
     
  2. I think you forgot to think of the empty slots.
    The item in the empty slots is equal to NULL so if you are trying to get the meta of that instance, it returns NULL.
    You need to add a NULL check in the For loop to check if the item is null and skip that one.

    Code:
                for(ItemStack item : content) {
                    if (item == null)  {
                        Bukkit.broadcastMessage("this slot is empty");
                        continue;
                    }
                    ItemMeta meta = item.getItemMeta();
                    Bukkit.broadcastMessage(meta.toString());
                }
     
Thread Status:
Not open for further replies.

Share This Page