Solved Can't add lore to item

Discussion in 'Spigot Plugin Development' started by Menfie, May 12, 2016.

  1. Here is my code

    Code (Text):
    ItemStack i = player.getItemInHand();
                                if (i.getType() != Material.AIR) {
                                    ItemMeta itemmeta = i.getItemMeta();
                                    List <String> lore = itemmeta.getLore();
                                    lore.add("asd");
                                    itemmeta.setLore(lore);
                                    i.setItemMeta(itemmeta);
                                    Auctions.items.put(i, money);
                                    commandSender.sendMessage("§e§l(!) §eDone!");
                                    player.getInventory().remove(i);

                                }
    So in code I have lore.add("asd"); and that generates nullpointerexception but if I use a blank list without item's lore I get no errors what is the problem? I can't see anything null.
     
  2. The item does not have a lore from the beginning, there for you get a null exception, create a new list instead of getting the list (that is null, because it does not exists) from the ItemMeta and set it with meta#setLore
     
    • Informative Informative x 1
  3. Wow I found my error I have to check if item has lore. Thanks for your help!
     
  4. You are still trying to get the lore from the item, lore.addAll(itemmeta.getLore());. The item doesn't have any lore, there for its returning null. And one more thing, your ItemMeta might be null too, check if the item has a ItemMeta and a lore before getting it.
     
    • Informative Informative x 1
  5. I'm not using lore.addall any more its like this:

    Code (Text):
    ItemMeta itemmeta = i.getItemMeta();
                                    if (itemmeta.getLore() != null) {
                                        List <String> lore = itemmeta.getLore();
                                        lore.add("§");
                                        itemmeta.setLore(lore);
                                        i.setItemMeta(itemmeta);
                                    }