Null Pointer Exception (solved)

Discussion in 'Spigot Plugin Development' started by Saday17, Jun 22, 2018.

  1. Having an issue I cannot seem to understand.

    The while loop stops running if the string is null and I am not sure as to why considering it is an if statement.

    I have tried several ways of solving this but I am unable to figure it out.

    Edit: I need it to check the display name and NOT the ItemStack because the lore will be different for every "coin"
  2. Apologies,

    line 275 is this:

    if(p.getInventory().getItem(i).getItemMeta().getDisplayName().equals(ChatColor.GOLD + "Big Coin"))
  3. Its probably returning a null ItemMeta. ps : I recommend separating each method by one line to determine which part of that statement is returning null .
  4. I had the same issue in the past, you have to check first if the item has an ItemMeta attached to it - in fact, a newly created item does not have an ItemMeta unless you apply enchantments, rename it or set an ItemMeta by yourself.

    To do this, you have to put this check before any other thing :
    Code (Java):
    if (!p.getInventory().getItem(i).hasItemMeta()) {
      // Some code
    In theory this will solve your issue
  5. I understand that it is returning null, I believe it is the display name returning null. I just need a way around that, I am unable to find one.

    Will try Zodiak
  6. ItemMeta#hasDisplayName() perhaps?
    • Winner Winner x 1
  7. If the item doesn't have an ItemMeta it will still throw a NPE because you're trying to use functions on a null instance of ItemMeta.
  8. AWESOME! Wish I realized hasItemMeta() was a thing.

    Thanks for the help! :)
  9. Of course, you'd check if it has an ItemMeta and then check that it has a displayname. I didn't speak against that, lol.