PlayerInteractEvent#getItem()#hasItemMeta returns null

Discussion in 'Plugin Development' started by Chr0mosom3, Mar 27, 2019.

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

    Chr0mosom3

    Code:
    ItemMeta meta = item.getItemMeta();
    Returns null pointer exception.
     
  2. Online

    timtower Administrator Administrator Moderator

    @MrDaniel Did you check if there even is an item?
     
  3. Offline

    Chr0mosom3

    @timtower, I didn't check but I did it with an item and without and same error. item#hasItemMeta also returns null
     
  4. Offline

    KarimAKL

    @MrDaniel If 'item.hasItemMeta()' throws a NullPointerException then 'item' is null. (could be an empty slot in the inventory or an ItemStack that hasn't been initialized yet)
     
  5. Offline

    Chr0mosom3

    @KarimAKL but even if he has an item in his hand it returns null.
     
  6. Online

    timtower Administrator Administrator Moderator

    Then what is the value of item? Print it to the console.
    What is item? Where does it get assigned?
     
  7. Offline

    Robnoo

    Can you paste the full method you wrote?
    Invoking any method on a null reference will throw a nullpointer exception. It's better to check if(item == null).
    Where is 'item' declared?
     
  8. Offline

    Chr0mosom3

    @Robnoo, please don't respond unless you have a solution that wasn't proposed before
     
  9. Online

    timtower Administrator Administrator Moderator

    Has this been solved or not?
    If so: please mark it as so.
    If not: please provide us with more code.
     
Thread Status:
Not open for further replies.

Share This Page