1.8.8 Killer returns NullPointerException

Discussion in 'Spigot Plugin Development' started by AwesomestGamer, Mar 19, 2020.

  1. I'm using the following code, but it's returning a NullPointer Exception. Can somebody help me with this?
    Code (Java):

    public class DeathMessage implements Listener {
        @EventHandler
        public void onDeathEvent(PlayerDeathEvent event) {
            String victim = event.getEntity().getDisplayName();
            if (event.getEntity().getKiller().getDisplayName() != null) {
                String assailant = event.getEntity().getKiller().getDisplayName();
                event.setDeathMessage(ChatColor.RED + victim + " was deleted by " + assailant);
                return;
            }
            event.setDeathMessage(ChatColor.RED + victim + " died of natural causes");
        }
    }
    EDIT: I used a try catch to fix this. However, I am still curious if there is any prettier solution for this.
    It return this exception:
    Code (Text):
    [20:05:45 ERROR]: Could not pass event PlayerDeathEvent to CrystalCapture v0.0.1 Alpha

    org.bukkit.event.EventException

        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:396) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.EntityPlayer.die(EntityPlayer.java:417) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.EntityLiving.damageEntity(EntityLiving.java:812) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.EntityHuman.damageEntity(EntityHuman.java:800) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.EntityPlayer.damageEntity(EntityPlayer.java:496) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.EntityLiving.O(EntityLiving.java:1183) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.Entity.K(Entity.java:327) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.EntityLiving.K(EntityLiving.java:169) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.Entity.t_(Entity.java:247) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.EntityLiving.t_(EntityLiving.java:1448) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.EntityHuman.t_(EntityHuman.java:173) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.EntityPlayer.l(EntityPlayer.java:285) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:382) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.PacketPlayInFlying.a(SourceFile:126) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.PacketPlayInFlying$PacketPlayInPosition.a(SourceFile:57) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_241]

        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_241]

        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_241]

    Caused by: java.lang.NullPointerException

        at to.us.blockverse.CrystalCapture.DeathMessage.onDeathEvent(DeathMessage.java:12) ~[?:?]

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_241]

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_241]

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_241]

        at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_241]

        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]

        ... 27 more
    Any other methods would be greatly appreciated. (So no try catch which I already am using).
     
  2. Choco

    Moderator

    Absolutely there is. You should never catch a NullPointerException. The only exception to this is if a library or some method out of your control is carelessly throwing an NPE instead of some other more appropriate exception.

    The reason you're getting an NPE is because Entity#getKiller() will return null if a Player did not kill said entity. You should be null checking the returned value and not assuming it's not null. You're null checking its name, though by that point it's already too late. You've invoked a method on a null pointer...
    This is no bueno. Null check getKiller() instead
     
  3. Whoops. Will fix. Thanks! Curious—do you have a link to an article that explains why not to catch NPEs?
     
  4. It’s more efficient to prevent exceptions than catch them
     
  5. Choco

    Moderator

    Not necessarily. The performance implications are negligible but the reason you shouldn't catch it is because it's easily resolvable without a try/catch. Good practices and a simple null check should solve your issue. The try/catch blocks aren't there as an easy-way-out for developer negligence
     
    • Agree Agree x 1
  6. Any exception should be fixed instead of catched (if possible). Basically you do not want any error to happen in your code. A catch doesn’t prevent the error to happen, all it does is catch it. You don’t wanna catch it, you wanna fix it to prevent any error. If your killer is null, there’s a reason and it’s because the entity doesn’t exist.
     
  7. Well, try-catch blocks exist for a reason. It's not that all errors should be "fixed", only certain runtime exceptions that can be prevented through good coding. For example, an IllegalArgumentException should not be caught, since you should make sure you're never using an illegal argument, but a NumberFormatException can be caught (if using Integer.parseInt()) because it represents a user input error.
     
  8. That’s why I mentioned if possible because in case of parsing and few other things you must use try catch since there are no other way to achieve it.
     
  9. Ah yeah, misread your post. That's the right idea.