0

I am using the inventory open event to get a chest location and trying to verify it is in fact a chest and then get the chests location.

Im using the following code and getting a null pointer error. Can anyone clarify why I am getting a null pointer error even when it appears the variable or data being passed to the variable isnt actually null?

public void onChestOpen(InventoryOpenEvent event) {
    Player player = (Player) event.getPlayer();
    Inventory inventory = event.getInventory();
    InventoryType invtype = inventory.getType();

    if( invtype != InventoryType.CHEST )
        return;

    Chest chest = (Chest) inventory.getHolder();
    Location chestLocation = chest.getLocation(); //this is line 266

    System.out.println("Chest Location "+chestLocation); // this is line 268

My Error:

Caused by: java.lang.NullPointerException
    at com.stoi2m1.chestloots.ChestLoots.onChestOpen(ChestLoots.java:266) ~[?:?]

However the System.out.print() method actually outputs

Chest Location (268) Location{world=CraftWorld{name=world},x=696.0,y=70.0,z=116.0,pitch=0.0,yaw=0.0}

Edit: Here is the actual error trace Maybe Im reading the error trace wrong..., because it actually prints out the location and then kicks out an error. Sny advise?

[18:19:42] [Server thread/INFO]: Chest Location (268) Location{world=CraftWorld{name=gta},x=696.0,y=70.0,z=116.0,pitch=0.0,yaw=0.0}
[18:19:42] [Server thread/ERROR]: Could not pass event InventoryOpenEvent to ChestLoots v1.0
org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callInventoryOpenEvent(CraftEventFactory.java:729) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.EntityPlayer.openContainer(EntityPlayer.java:663) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.entity.CraftHumanEntity.openInventory(CraftHumanEntity.java:185) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at com.stoi2m1.chestloots.ChestLoots.onChestOpen(ChestLoots.java:375) [ChestLoots.jar:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_111]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_111]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_111]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callInventoryOpenEvent(CraftEventFactory.java:729) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.EntityPlayer.openContainer(EntityPlayer.java:663) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.BlockChest.interact(SourceFile:339) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerInteractManager.interact(PlayerInteractManager.java:496) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:759) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_111]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_111]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_111]
Caused by: java.lang.NullPointerException
    at com.stoi2m1.chestloots.ChestLoots.onChestOpen(ChestLoots.java:266) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_111]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_111]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_111]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_111]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8-R0.1.jar:git-Spigot-db6de12-18fbb24]
stoi2m1
  • 786
  • 5
  • 14
  • The only thing I can think of is that `inventory.getHolder()` occasionally returns `null`. Why that happens is something you'll have to debug. – Makoto Jan 17 '17 at 00:03
  • This returns null pointer every time and outputs a good location every time, this is not a duplicate – stoi2m1 Jan 17 '17 at 00:12
  • I have added my error trace, maybe Im evaluating the error in the wrong order of events and my error is actually somewhere else in my code. Any thoughts? – stoi2m1 Jan 17 '17 at 00:19
  • 1
    @Makoto This question is not a duplicate. It concerns itself with why the OP is getting an NPE using the Spigot API, not the meaning of an NPE. – Frelling Jan 17 '17 at 04:37
  • By all rights, your method should work. I copied it verbatim into a test plugin. It performs as expected without throwing an NPE using Spigot API and server for 1.11.2. I notice that your server is running version 1.8. Are you sure that you are using the same Spigot API version when compiling your plugin? – Frelling Jan 17 '17 at 05:00
  • @Frelling, thank you for the support and advice. I have verified my build path is using both spigot and craftbukkit 1.8.8 and my server is running 1.8.8. Thanks for the validation of my current code it reassuring I'm not losing my mind. – stoi2m1 Jan 17 '17 at 06:26
  • @Frelling your test lead me to test just the portion of code I shared. I found the code in fact works with no issue. I then realized further down in my code I was opening a new inventory instance which triggered the event again. Since the new instance didn't actually exist it doesn't have a location and creates a null pointer after the server print. Why line 268 appears in the error before 266. Thanks again for testing my code. If you can make your comment an answer I will accept it and add my comment for the final solution there. – stoi2m1 Jan 17 '17 at 07:51
  • Ah ha, that would certainly do it! Glad you discovered it. Sadly, I can't add an answer since Makoto was a bit overzealous flagging this as a duplicate, but I appreciate the offer. – Frelling Jan 17 '17 at 08:16

0 Answers0