NullPointerException 的所有可能位置? (SpigotMC)
All the possible locations of a NullPointerException? (SpigotMC)
所以我有一个名为 setOwner 的函数,如图所示...
public void setOwner(Player player){
this.setGoalTarget((EntityLiving)((CraftPlayer)player).getHandle(), EntityTargetEvent.TargetReason.CUSTOM, false);
}
我在以下地方使用了这个函数:
pet.setOwner(e.getEntity().getKiller());
pet.setName("&a" + e.getEntity().getKiller().getName() + "'s pet");
因此,当此代码为 运行 时,它会给出如下所示的 NullPointerException
:
[21:14:43] [Server thread/ERROR]: Could not pass event EntityDeathEvent to Necro v1.0
org.bukkit.event.EventException: null
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.java:319) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at org.bukkit.craftbukkit.v1_16_R3.event.CraftEventFactory.callEntityDeathEvent(CraftEventFactory.java:762) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityLiving.d(EntityLiving.java:1447) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityLiving.die(EntityLiving.java:1395) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityLiving.damageEntity(EntityLiving.java:1241) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityEnderDragon.dealDamage(EntityEnderDragon.java:530) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityEnderDragon.a(EntityEnderDragon.java:500) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityComplexPart.damageEntity(SourceFile:47) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityHuman.attack(EntityHuman.java:1068) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityPlayer.attack(EntityPlayer.java:1632) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:2001) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.PacketPlayInUseEntity.a(SourceFile:74) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.PacketPlayInUseEntity.a(SourceFile:13) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.PlayerConnectionUtils.lambda[=12=](PlayerConnectionUtils.java:19) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.TickTask.run(SourceFile:18) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeTask(SourceFile:144) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeNext(SourceFile:118) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.MinecraftServer.bb(MinecraftServer.java:942) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext(MinecraftServer.java:935) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.MinecraftServer.sleepForTick(MinecraftServer.java:919) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:851) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.MinecraftServer.lambda[=12=](MinecraftServer.java:164) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at java.lang.Thread.run(Thread.java:832) [?:?]
Caused by: java.lang.ClassCastException: class net.minecraft.server.v1_16_R3.EntityEnderDragon cannot be cast to class net.minecraft.server.v1_16_R3.EntityCreature (net.minecraft.server.v1_16_R3.EntityEnderDragon and net.minecraft.server.v1_16_R3.EntityCreature are in unnamed module of loader 'app')
at org.bukkit.craftbukkit.v1_16_R3.entity.CraftCreature.getHandle(CraftCreature.java:14) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at org.bukkit.craftbukkit.v1_16_R3.entity.CraftCreature.getHandle(CraftCreature.java:1) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at com.TheRealBee.e.listener.PetEvent.onEntityDeath(PetEvent.java:18) ~[?:?]
at jdk.internal.reflect.GeneratedMethodAccessor22.invoke(Unknown Source) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:564) ~[?:?]
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.java:315) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
... 27 more
...为什么?所有可能的 NullPointerException
在哪里?我检查了警告但没有发现任何东西所以我已经这样做了,不要告诉我“哦 ViciousBee 你检查警告了吗......”
编辑:正确的错误是:
Caused by: java.lang.NullPointerException: Cannot invoke "org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer.getHandle()" because "player" is null
at com.TheRealBee.e.CustomCreature.setOwner(CustomCreature.java:27) ~[?:?]
at com.TheRealBee.e.listener.PetEvent.onEntityDeath(PetEvent.java:21) ~[?:?]
at jdk.internal.reflect.GeneratedMethodAccessor3.invoke(Unknown Source) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:564) ~[?:?]
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.java:315) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
... 34 more
我知道查看和阅读堆栈跟踪可能有点难以识别实际错误是什么或它在您的代码中发生的位置。
查看堆栈跟踪,您会看到两个异常:
org.bukkit.event.EventException
。第一个只是告诉您异常发生在事件或侦听器中。还有其他类型,例如命令异常:org.bukkit.command.CommandException
.
java.lang.ClassCastException
。这是您应该关注的主要问题。
根据 documentation,此异常告诉您代码已尝试将对象强制转换为它不是实例的子class。
A ClassCastException
如果你正在转换,比方说一个整数,一个字符串,就像这样:
Object x = new Integer(0);
System.out.println((String)x);
会抛出异常,因为String不是Integer的实例。
在您的代码中,ClassCastException
出现在 PetEvent
class 的第 18 行,如您的堆栈跟踪所示:
Caused by: java.lang.ClassCastException: class net.minecraft.server.v1_16_R3.EntityEnderDragon cannot be cast to class net.minecraft.server.v1_16_R3.EntityCreature (net.minecraft.server.v1_16_R3.EntityEnderDragon and net.minecraft.server.v1_16_R3.EntityCreature are in unnamed module of loader 'app')
...
at com.TheRealBee.e.listener.PetEvent.onEntityDeath(PetEvent.java:18) ~[?:?]
它还告诉你,你正在将 EntityEnderDragon
转换为 EntityCreature
。
所以我有一个名为 setOwner 的函数,如图所示...
public void setOwner(Player player){
this.setGoalTarget((EntityLiving)((CraftPlayer)player).getHandle(), EntityTargetEvent.TargetReason.CUSTOM, false);
}
我在以下地方使用了这个函数:
pet.setOwner(e.getEntity().getKiller());
pet.setName("&a" + e.getEntity().getKiller().getName() + "'s pet");
因此,当此代码为 运行 时,它会给出如下所示的 NullPointerException
:
[21:14:43] [Server thread/ERROR]: Could not pass event EntityDeathEvent to Necro v1.0
org.bukkit.event.EventException: null
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.java:319) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:589) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:576) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at org.bukkit.craftbukkit.v1_16_R3.event.CraftEventFactory.callEntityDeathEvent(CraftEventFactory.java:762) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityLiving.d(EntityLiving.java:1447) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityLiving.die(EntityLiving.java:1395) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityLiving.damageEntity(EntityLiving.java:1241) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityEnderDragon.dealDamage(EntityEnderDragon.java:530) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityEnderDragon.a(EntityEnderDragon.java:500) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityComplexPart.damageEntity(SourceFile:47) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityHuman.attack(EntityHuman.java:1068) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.EntityPlayer.attack(EntityPlayer.java:1632) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:2001) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.PacketPlayInUseEntity.a(SourceFile:74) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.PacketPlayInUseEntity.a(SourceFile:13) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.PlayerConnectionUtils.lambda[=12=](PlayerConnectionUtils.java:19) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.TickTask.run(SourceFile:18) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeTask(SourceFile:144) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeNext(SourceFile:118) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.MinecraftServer.bb(MinecraftServer.java:942) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext(MinecraftServer.java:935) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.MinecraftServer.sleepForTick(MinecraftServer.java:919) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:851) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at net.minecraft.server.v1_16_R3.MinecraftServer.lambda[=12=](MinecraftServer.java:164) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at java.lang.Thread.run(Thread.java:832) [?:?]
Caused by: java.lang.ClassCastException: class net.minecraft.server.v1_16_R3.EntityEnderDragon cannot be cast to class net.minecraft.server.v1_16_R3.EntityCreature (net.minecraft.server.v1_16_R3.EntityEnderDragon and net.minecraft.server.v1_16_R3.EntityCreature are in unnamed module of loader 'app')
at org.bukkit.craftbukkit.v1_16_R3.entity.CraftCreature.getHandle(CraftCreature.java:14) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at org.bukkit.craftbukkit.v1_16_R3.entity.CraftCreature.getHandle(CraftCreature.java:1) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
at com.TheRealBee.e.listener.PetEvent.onEntityDeath(PetEvent.java:18) ~[?:?]
at jdk.internal.reflect.GeneratedMethodAccessor22.invoke(Unknown Source) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:564) ~[?:?]
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.java:315) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
... 27 more
...为什么?所有可能的 NullPointerException
在哪里?我检查了警告但没有发现任何东西所以我已经这样做了,不要告诉我“哦 ViciousBee 你检查警告了吗......”
编辑:正确的错误是:
Caused by: java.lang.NullPointerException: Cannot invoke "org.bukkit.craftbukkit.v1_16_R3.entity.CraftPlayer.getHandle()" because "player" is null
at com.TheRealBee.e.CustomCreature.setOwner(CustomCreature.java:27) ~[?:?]
at com.TheRealBee.e.listener.PetEvent.onEntityDeath(PetEvent.java:21) ~[?:?]
at jdk.internal.reflect.GeneratedMethodAccessor3.invoke(Unknown Source) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:564) ~[?:?]
at org.bukkit.plugin.java.JavaPluginLoader.execute(JavaPluginLoader.java:315) ~[Server.jar:2991-Spigot-018b9a0-f3f3094]
... 34 more
我知道查看和阅读堆栈跟踪可能有点难以识别实际错误是什么或它在您的代码中发生的位置。
查看堆栈跟踪,您会看到两个异常:
org.bukkit.event.EventException
。第一个只是告诉您异常发生在事件或侦听器中。还有其他类型,例如命令异常:org.bukkit.command.CommandException
.java.lang.ClassCastException
。这是您应该关注的主要问题。 根据 documentation,此异常告诉您代码已尝试将对象强制转换为它不是实例的子class。
A ClassCastException
如果你正在转换,比方说一个整数,一个字符串,就像这样:
Object x = new Integer(0);
System.out.println((String)x);
会抛出异常,因为String不是Integer的实例。
在您的代码中,ClassCastException
出现在 PetEvent
class 的第 18 行,如您的堆栈跟踪所示:
Caused by: java.lang.ClassCastException: class net.minecraft.server.v1_16_R3.EntityEnderDragon cannot be cast to class net.minecraft.server.v1_16_R3.EntityCreature (net.minecraft.server.v1_16_R3.EntityEnderDragon and net.minecraft.server.v1_16_R3.EntityCreature are in unnamed module of loader 'app')
...
at com.TheRealBee.e.listener.PetEvent.onEntityDeath(PetEvent.java:18) ~[?:?]
它还告诉你,你正在将 EntityEnderDragon
转换为 EntityCreature
。