Search code examples
javabukkit

Why do I get an error when I try to click outside the inventory / in the inventory where there's no item


I am using Spigot 1.14 and when I try to click in the inventory or outside the inventory where there is no item I get an error.

I have tried with different methods such as:

e.getCurrentItem().getItemMeta().getDisplayName()
e.getCurrentItem().getItemMeta().hasDisplayName()

and some other methods but none of them seem to work.

if (e.getCurrentItem().getItemMeta().getDisplayName() != null && (e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase("§eMystery Box"))) {

When I click outside the inventory or in the inventory where there is no item I get this error message: https://pastebin.com/v0wDB2gp

Saying that it could not pass the inventory click event.


Solution

  • From the code and the stacktrace everything I can say is that either e.getCurrentItem().getItemMeta() or e.getCurrentItem() is returning null.
    Stacktrace

    Could not pass event InventoryClickEvent to MysteryBox v1.0-SNAPSHOT
    org.bukkit.event.EventException: null
            ...
    Caused by: java.lang.NullPointerException
            at com.chefren.mysterybox.clickevent.onclick(clickevent.java:23) ~[?:?]
            ...
    

    See also
    What is a stack trace, and how can I use it to debug my application errors?
    What is a NullPointerException, and how do I fix it?