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
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-798f32d-0cd0397]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-798f32d-0cd0397]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-798f32d-0cd0397]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-798f32d-0cd0397]
            at org.bukkit.craftbukkit.v1_9_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:231) [spigot.jar:git-Spigot-798f32d-0cd0397]
            at org.bukkit.craftbukkit.v1_9_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:198) [spigot.jar:git-Spigot-798f32d-0cd0397]
            at org.bukkit.craftbukkit.v1_9_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:194) [spigot.jar:git-Spigot-798f32d-0cd0397]
            at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1387) [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$1.run(SourceFile:13) [spigot.jar:git-Spigot-798f32d-0cd0397]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_91]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?: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(MinecraftServer.java:726) [spigot.jar:git-Spigot-798f32d-0cd0397]
            at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-798f32d-0cd0397]
            at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot.jar:git-Spigot-798f32d-0cd0397]
            at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot.jar:git-Spigot-798f32d-0cd0397]
            at java.lang.Thread.run(Thread.java:745) [?:1.8.0_91]
    Caused by: java.lang.NullPointerException
            at com.perkelle.dev.hub.listeners.Interact.interactEvent(Interact.java:14) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_91]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_91]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_91]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_91]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-798f32d-0cd0397]
            ... 18 more
     
    and all of my Interact class:
    http://hastebin.com/uhezahiquc.avrasm

    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));

    or

    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.