.add() on a List<String> throws NullPointer

Discussion in 'Plugin Development' started by xXCryptoFreakXx, Feb 1, 2014.

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

    xXCryptoFreakXx

    Hi, I'm writing a command for my plugin that allows for simple multi-line lore editing of an item. However, when I get the lore from an item's meta in the form of a String List, and then try to use lore.add(newline); it throws a NullPointerException.

    Heres my pathetic attempt to debug it, along with screenshots from the console and ingame:

    Code:java
    1. sender.sendMessage("§eDEBUG: " + itemname);
    2. ItemMeta newmeta = item.getItemMeta();
    3. List<String> lore = newmeta.getLore();
    4. try {
    5. lore.add(itemname);
    6. }
    7. {
    8. sender.sendMessage("§4ERROR: NullPointerException!");
    9. sender.sendMessage("§4contacting NSA...");
    10. sender.sendMessage("§4Printing error info:");
    11. sender.sendMessage("§c" + e.getStackTrace());
    12. sender.sendMessage("§e" + e.getCause());
    13. sender.sendMessage("§8" + e.getMessage());
    14. sender.sendMessage("§7" + e.getLocalizedMessage());
    15. }


    [​IMG]

    Thanks!
     
  2. Offline

    werter318

    The item doesn't have lore yet, that's why it's throwing a npe
     
  3. Offline

    Iaccidentally

    You need to set the lore after adding it.
    Code:java
    1. newmeta.setLore(lore);
     
  4. I think you have something like:

    If (item.getItemMeta().getLore().get(0).matches("lore)")

    Change that to:


    If (item.getItemMeta().getLore().get(0).matches("lore)" && item.getItemMeta().getLore().get(0) != null )
     
  5. Offline

    xXCryptoFreakXx


    This is the whole snippet of code:
    Code:java
    1. if (args.length > 1) {
    2. if (args[0].equalsIgnoreCase("add")) {
    3. String itemname = null;
    4. for (int str = 1; str < args.length; str++) {
    5. sb.append(args[str]).append(" ");
    6. }
    7. sb.deleteCharAt(sb.length() - 1);
    8. itemname = sb.toString();
    9. itemname = itemname.replace("&", "§");
    10. sender.sendMessage("§eDEBUG: " + itemname);
    11. ItemMeta newmeta = item.getItemMeta();
    12. {
    13.  
    14. }
    15. List<String> lore = newmeta.getLore();
    16. newmeta.getLore();
    17. try {
    18. lore.add(itemname);
    19. }
    20. {
    21. sender.sendMessage("§4ERROR: NullPointerException!");
    22. sender.sendMessage("§4contacting NSA...");
    23. sender.sendMessage("§4Printing error info:");
    24. sender.sendMessage("§c" + e.getStackTrace());
    25. sender.sendMessage("§e" + e.getCause());
    26. sender.sendMessage("§8" + e.getMessage());
    27. sender.sendMessage("§7" + e.getLocalizedMessage());
    28.  
    29. }
    30. newmeta.setLore(lore);
    31. item.setItemMeta(newmeta);
    32. sender.sendMessage("§3iLore Added! (Line: " + lore.size() + ")");
    33.  
    34. }


    Anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page