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
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-96235ab-2aa5ac6]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-96235ab-2aa5ac6]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-96235ab-2aa5ac6]
        .
        .
        .
    Caused by: java.lang.NullPointerException
        at me.lemmy.listener.PlayerEvents.onPlayerInteract(PlayerEvents.java:154) ~[?:?]
        at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) ~[?:?]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_65]
        at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_65]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-96235ab-2aa5ac6]
        ... 17 more
     
    And here's the main code around the line said:
    Code (Text):

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event)
    {
        if (!(event.getAction() == Action.RIGHT_CLICK_AIR ||
                event.getAction() == Action.RIGHT_CLICK_BLOCK))
            return;

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

        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?

    Thanks.
     
  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