Solved Getting a NullPointerException but checking first if its null .-.

Discussion in 'Spigot Plugin Development' started by The_Red_Freak, May 21, 2017.

  1. Hello,

    i'm getting at the second if statement a NullPointerException

    (its in an PlayerInteractEvent method)

    item = the item player has in his mainhand
    secondSpellItem = is actually null

    Code (Text):
     
    if(secondSpellItem != null){
        if(item.getItemMeta().getDisplayName().equalsIgnoreCase(secondSpellItem.getItemMeta().getDisplayName())) {
            try {
                SpellMethods.class.getMethod(thirdMethod).invoke(new SpellMethods(), ev.getPlayer());
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
     
     
  2. Can you elaborate on what your trying to actually do?
     
  3. hi,
    i want to check the item which the player has in it's main hand and execute a method based on a variable declared within the class
     
  4. Check if the first and second Item have a metadata using ItemStack#hasMetaData
     
    • Like Like x 1
  5. And check ItemMeta#hasDisplayName
     
  6. Probably because it throws null if you're not holding an item, or if the item doesn't have a name.

    ItemStack#hasItemMeta() *

    and

    ItemMeta#hasDisplayName()
     
  7. he did, actually it's not what he wanted to do it is something he wanted to fix but didn't know how.
     
    • Like Like x 1
  8. Thanks that helped!