[Solved] NullPointerException?

Discussion in 'Spigot Plugin Development' started by BryceTheCoder, May 13, 2015.

  1. Hey guys, it's currently 3 AM so I'm probably missing something easy, but I can't see it.
    Why am I getting a null pointer?

    http://pastebin.com/JhBmz6YA

    if (cmd.getName().equalsIgnoreCase("setlore")) {
    if (args.length < 1) {
    p.sendMessage("/setlore X LoreTextHere");
    return true;
    }
    ItemStack i = p.getItemInHand();
    ItemMeta im = i.getItemMeta();
    if (im.getLore().get(Integer.parseInt(args[0]) - 1) == null)
    im.getLore().add(Integer.parseInt(args[0]) - 1, args[1]);
    else
    im.getLore().set(Integer.parseInt(args[0]) - 1, args[1]);
    i.setItemMeta(im);
    return true;
    }
     
  2. gigosaurus

    Supporter

    The item the player is holding might be null, or it might contain no lore - both of which you should check before trying to do anything with it. Also I believe you will need to use setLore to actually modify the lore.
     
  3. You need to set your lore to your itemmeta.
     
  4. Go to this line
    Class: BtcMain.java
    Line: 153
    Code (Text):
    Caused by: java.lang.NullPointerException
               at me.BryceTheCoder.BtcMain.onCommand(BtcMain.java:153) ~[?:?]
    and pls post your code using insert > Code.
     
    #4 ToPoEdiTs, May 13, 2015
    Last edited: May 13, 2015
  5. The item or the ItemMeta is null most likely.
     
  6. ItemMeta wouldn't be null unless it was Air
     
  7. It would probably be best to check if the item is NOT null and DOES have a lore,


    Code (Text):
    if(player.getItemInHand() != null && player.getItemInHand().hasItemMeta()) {

    //Here you can specify your itemStack (held item)

    if(item.getItemMeta().hasLore()) {
    Hope this helped :)
     
  8. Solved and fixed. Thanks.