Solved NullPointerException ??

Discussion in 'Spigot Plugin Development' started by Stitchie, Sep 15, 2020 at 10:56 AM.

  1. So i have debugged my code down to this:
    Code (Java):
        @EventHandler
        public void openSpecialEnchantGUI(PlayerInteractEvent event) {

            if (event.getClickedBlock().getType() == Material.ENCHANTMENT_TABLE) {
    // open special inventory and do stuff
    }
    }
     
    So just by executing this code im getting a NullPointerException for some reason that doesn't actually happen when i click the enchant table but almost randomly some time afterwards or honestly im not even sure if you need to click it to get this error.

    Code (Text):
    [11:48:13 ERROR]: Could not pass event PlayerInteractEvent to EnchantPlugin v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[patched.jar:git-PaperSpigot-"4c7641d"]
            at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:78) ~[patched.jar:git-PaperSpigot-"4c7641d"]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[patched.jar:git-PaperSpigot-"4c7641d"]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:517) [patched.jar:git-PaperSpigot-"4c7641d"]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:502) [patched.jar:git-PaperSpigot-"4c7641d"]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [patched.jar:git-PaperSpigot-"4c7641d"]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:195) [patched.jar:git-PaperSpigot-"4c7641d"]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191) [patched.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1227) [patched.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:18) [patched.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.PacketPlayInArmAnimation.a(PacketPlayInArmAnimation.java:1) [patched.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [patched.jar:git-PaperSpigot-"4c7641d"]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_261]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_261]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [patched.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:774) [patched.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:378) [patched.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:713) [patched.jar:git-PaperSpigot-"4c7641d"]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:616) [patched.jar:git-PaperSpigot-"4c7641d"]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_261]
    Caused by: java.lang.NullPointerException
            at me.plugin.manager.Enchanter.openSpecialEnchantGUI(Enchanter.java:15) ~[?:?]
            at sun.reflect.GeneratedMethodAccessor1176.invoke(Unknown Source) ~[?:?]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_261]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_261]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:300) ~[patched.jar:git-PaperSpigot-"4c7641d"]
            ... 19 more
    Anyone know why this occurs? I'm guessing its related to how i get the block but besides this error spam in the console it works as intended.
     
  2. because you were clicking the air not a block at that time
    to fix this just do this at the very beginning

    Code (Java):
    if (event.getClickedBlock() == null) return;
     
  3. Oh thanks! I just realized that more or less 1 minute ago but your check works better.
     
    • Like Like x 1
  4. As a bonus you can specify the actions beforehand, because your looking for a block not an item

    Code (Java):
    if (event.getAction().equals(ACTION.RIGHT_CLICK_BLOCK)) {
    // do stuff
    }
     
  5. Isn't it better to use == instead of .equals? Since Action.RIGHT_CLICK_BLOCK is an enum
     
  6. Functionality wise it's the same thing
     
    • Agree Agree x 1
  7. Of course but == will be type safe and faster and can prevent NPE's