我正在使用库存打开事件来获取胸部位置,并尝试验证它实际上是胸部,然后获取胸部位置。
我使用以下代码并获得空指针错误。任何人都可以澄清为什么我得到一个空指针错误,即使它看起来传递给变量的变量或数据实际上是空的?
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
我的错误:
Caused by: java.lang.NullPointerException
at com.stoi2m1.chestloots.ChestLoots.onChestOpen(ChestLoots.java:266) ~[?:?]
但是,System.out.print()方法实际输出
Chest Location (268) Location{world=CraftWorld{name=world},x=696.0,y=70.0,z=116.0,pitch=0.0,yaw=0.0}
编辑:这是实际的错误跟踪 也许我正在读取错误跟踪错误...,因为它实际打印出位置然后踢出错误。 Sny建议?
[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]