Solved Item null after null check

Discussion in 'Spigot Plugin Development' started by DotRar, Jun 7, 2016.

  1. I'm getting a weird error when I left / right click the air with an empty hand, even though I have done a null check. Here's the error:

    Code (Text):
    Could not pass event PlayerInteractEvent to PerkelleHub v1.0
            at$1.execute( ~[spigot.jar:git-Spigot-798f32d-0cd0397]
            at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot.jar:git-Spigot-798f32d-0cd0397]
            at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot.jar:git-Spigot-798f32d-0cd0397]
            at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot.jar:git-Spigot-798f32d-0cd0397]
            at org.bukkit.craftbukkit.v1_9_R2.event.CraftEventFactory.callPlayerInteractEvent( [spigot.jar:git-Spigot-798f32d-0cd0397]
            at org.bukkit.craftbukkit.v1_9_R2.event.CraftEventFactory.callPlayerInteractEvent( [spigot.jar:git-Spigot-798f32d-0cd0397]
            at org.bukkit.craftbukkit.v1_9_R2.event.CraftEventFactory.callPlayerInteractEvent( [spigot.jar:git-Spigot-798f32d-0cd0397]
            at net.minecraft.server.v1_9_R2.PlayerConnection.a( [spigot.jar:git-Spigot-798f32d-0cd0397]
            at net.minecraft.server.v1_9_R2.PacketPlayInArmAnimation.a(SourceFile:32) [spigot.jar:git-Spigot-798f32d-0cd0397]
            at net.minecraft.server.v1_9_R2.PacketPlayInArmAnimation.a(SourceFile:9) [spigot.jar:git-Spigot-798f32d-0cd0397]
            at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$ [spigot.jar:git-Spigot-798f32d-0cd0397]
            at java.util.concurrent.Executors$ [?:1.8.0_91]
            at [?:1.8.0_91]
            at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-798f32d-0cd0397]
            at net.minecraft.server.v1_9_R2.MinecraftServer.D( [spigot.jar:git-Spigot-798f32d-0cd0397]
            at net.minecraft.server.v1_9_R2.DedicatedServer.D( [spigot.jar:git-Spigot-798f32d-0cd0397]
            at net.minecraft.server.v1_9_R2.MinecraftServer.C( [spigot.jar:git-Spigot-798f32d-0cd0397]
            at [spigot.jar:git-Spigot-798f32d-0cd0397]
            at [?:1.8.0_91]
    Caused by: java.lang.NullPointerException
            at ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
            at sun.reflect.NativeMethodAccessorImpl.invoke( ~[?:1.8.0_91]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[?:1.8.0_91]
            at java.lang.reflect.Method.invoke( ~[?:1.8.0_91]
            at$1.execute( ~[spigot.jar:git-Spigot-798f32d-0cd0397]
            ... 18 more
    and all of my Interact class:

    Line 14:
    Code (Text):
    if(e.getItem().getType() != null) {
  2. Can we have the whole code of the event?
  3. That's because e.getItem() is null, therefore you cannot use e.getItem().getType()
    • Winner Winner x 3
  4. That's all of it
  5. No matter what you click on in an inventory, the item type will never be null. The item type for emtpy slots will be AIR. So you either have to check if the material/type is air, or check if the item itself, not the type, is null.

    Use one of the following:
    Code (Text):
    if (e.getItem.equals(null));


    if (e.getItem().getType() == Material.AIR);
  6. Basically read what @Ferdz said.
  7. You are 100% wrong sir. That will still throw exceptions. The item is able to be null. You need to do what @Ferdz said.
    • Agree Agree x 1
  8. Which part of it is wrong? I've used Material.AIR as a null item check in the past and it has always worked.
    #8 Hunky524, Jun 7, 2016
    Last edited: Jun 7, 2016
  9. Works! Thanks!
  10. AIR is not null, AIR is AIR..
    Null means that the object simply does not exist - it has no place in memory and no data at all.
  11. @Ferdz @FlyingLlama Allocating the variable takes memory as there is still a reference pointing to null, but it is just a pointer's worth of memory, not an entire object.
  12. However, for some reason in Spigot, getting the material of an empty slot returns AIR not null. I only said this because multiple times for me in the past, just checking if the ItemStack was null and not the material/type would still give me random NPEs, but if I checked if the material of the ItemStack was AIR, then I never got an NPE relating to a clicked item.
  13. Getting a block that is empty returns Material.AIR. Getting an item in an inventory returns null.

    There is never a time that Air can be added to an inventory. I welcome you to go try it.
  14. All I'm saying is when I've made inventory click events and I don't add a check that makes sure the clicked item isn't AIR, I get NPEs.