Inventory Contents returning null?

Discussion in 'Plugin Development' started by AstramG, Apr 21, 2014.

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

    AstramG

    Hello, I have this code here:
    Code:java
    1. public Inventory getInventory(Player player) {
    2. List<ItemStack> contents = new LinkedList<ItemStack>();
    3. contents.addAll(Arrays.asList(kitInventory.getContents()));
    4. for (ItemStack item : contents) {
    5. ItemMeta meta = item.getItemMeta();
    6. String kitName = ChatColor.stripColor(meta.getDisplayName()).split(" ")[0];
    7. Kit kit = getKit(kitName);
    8. meta.setLore(kit.getLore());
    9. List<String> lore = meta.getLore();
    10. lore.add("Test!");
    11. meta.setLore(lore);
    12. item.setItemMeta(meta);
    13. }
    14. return kitInventory;
    15. }

    And whenever I try to use this method it gives me a null pointer exception on the line where it gets the meta. I think this has to do with the contents list not being able to be read, but when I try to System.out.println(contents.toString()) it works fine! I'm very confused and am seeking help. Error log: http://pastebin.com/dLe3rXnp

    LINE 38 is where it gets the item's meta. I'm also sure that the inventories contents isn't equal to null.

    Thank you!
     
  2. Offline

    beeselmane

    The item may not have meta.. check if it's null, and if it is then just create a new ItemMeta()
     
  3. Offline

    AstramG

    The item does have ItemMeta
     
  4. Offline

    beeselmane

    Well first of all, the inventory space itself can be null. If that is true, the meta will also be null. You should check if the item is null in the beginning of the for loop, and if it is, continue;
     
  5. Offline

    AstramG

    I'm pretty sure that the contents just return the filled in spots.
    Boy was I wrong xD! Well thank you beeselmane, major fail on my part.
     
  6. Offline

    beeselmane

    Having a null check won't do anything bad :p

    Documentation of the method says: "Returns all ItemStacks from the inventory." so I'm assuming it includes null ItemStacks
     
    AstramG likes this.
Thread Status:
Not open for further replies.

Share This Page