Question about getItem() behavior in PlayerInteractEvent

Discussion in 'Spigot Plugin Development' started by Camo341, Mar 22, 2017.

  1. Hello. So, I have an event handler for PlayerInteractEvent. Everything's fine, all but an NPE that shows up in the console when I right click a block:

    Code (Text):
    [11:04:38 ERROR]: Could not pass event PlayerInteractEvent to Kits v1.0.0
        at$1.execute( ~[spigot.jar:git-Spigot-96235ab-2aa5ac6]
        at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot.jar:git-Spigot-96235ab-2aa5ac6]
        at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot.jar:git-Spigot-96235ab-2aa5ac6]
    Caused by: java.lang.NullPointerException
        at me.lemmy.listener.PlayerEvents.onPlayerInteract( ~[?:?]
        at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:1.8.0_65]
        at java.lang.reflect.Method.invoke( ~[?:1.8.0_65]
        at$1.execute( ~[spigot.jar:git-Spigot-96235ab-2aa5ac6]
        ... 17 more
    And here's the main code around the line said:
    Code (Text):

    public void onPlayerInteract(PlayerInteractEvent event)
        if (!(event.getAction() == Action.RIGHT_CLICK_AIR ||
                event.getAction() == Action.RIGHT_CLICK_BLOCK))

        ItemStack item = event.getItem();
        if (item.getItemMeta() == null || item.getItemMeta().getDisplayName() == null)

        Player player = event.getPlayer();
        String name = item.getItemMeta().getDisplayName();
    Line 154 is the item null checks. I get now that I have to null check the item itself before checking its meta, so I'll add that, but I don't understand. In some of the cases when the exception shows up, the player has something in his hand, so why does getItem() return null? Also, how come it only gives the error on right clicking a block and not when right clicking air?

  2. For your first question, it might be more robust to use the item in the player's hand, rather than the item in the event? I'm not really sure when the event has that item set, in fact I'm not sure I knew it was there ... guessing that it's only there if you're using a tool that has some right-click function?

    For your second question, the interact event doesn't fire if not holding an item and right-clicking in the air, so that's probably why you didn't run into the error in that case.
  3. I figured I'd have had to use getItem cause the docs said it represents the item in hand. The plugin stopped throwing the exception after adding the item == null check, but yeah I didn't think of using PlayerInventory's getItemInHand().

    Edit: a word
  4. Used hasItem() ???
  5. It's not .getItem() returning null, it's .getItemMeta() returning null. Just null check that and you should be good to go.
  6. I'm guessing they figured this out... like 2 1/2 months ago :ROFLMAO:
    • Winner Winner x 1
    • Informative Informative x 1
  7. add a year to that, haha :p
  8. Oh, wow, yeah.... ha I missed that. I guess it explains why I had absolutely no memory of this thread! o_O

    But... Spigot notifications... they never forget.
    • Agree Agree x 1
  9. Well this is embarassing... Only checked the time of the previous post... My apologies.
    • Friendly Friendly x 1

Share This Page