Solved How do I fix java.lang.NullPointerException: null in PlayerInteractEvent?

Discussion in 'Spigot Plugin Development' started by Diaburo, Jun 20, 2021.

  1. Everything in the event works perfectly, but when the event happens this error appears in the console:
    Code (Text):
    [16:25:54 ERROR]: Could not pass event PlayerInteractEvent to Manhunt v1.0
    java.lang.NullPointerException: null
            at dev.diaburo.manhunt.Manhunt.onPlayerUse(Manhunt.java:74) ~[?:?]
            at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor4.execute(Unknown Source) ~[?:?]
            at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:69) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:607) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at org.bukkit.craftbukkit.v1_16_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:531) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at org.bukkit.craftbukkit.v1_16_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:494) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at org.bukkit.craftbukkit.v1_16_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:489) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.PlayerInteractManager.a(PlayerInteractManager.java:209) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:1704) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.PacketPlayInBlockDig.a(SourceFile:40) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.PacketPlayInBlockDig.a(SourceFile:10) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.PlayerConnectionUtils.lambda$ensureMainThread$1(PlayerConnectionUtils.java:55) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.TickTask.run(SourceFile:18) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.MinecraftServer.bb(MinecraftServer.java:1311) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.MinecraftServer.executeNext(MinecraftServer.java:1304) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1379) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:1120) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$a$0(MinecraftServer.java:290) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
            at java.lang.Thread.run(Thread.java:748) [?:1.8.0_282]
    This is the event's code:
    Code (Java):
    @EventHandler
        public void onPlayerUse(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            ItemStack compass = event.getItem();
            if (speedrunner != null) {
                if (compass.getType() == Material.getMaterial("COMPASS")) {
                    if (player.getWorld() == speedrunner.getWorld()) {
                        CompassMeta compassMeta = (CompassMeta) compass.getItemMeta();
                        compassMeta.setLodestoneTracked(false);
                        compassMeta.setLodestone(speedrunner.getLocation());
                        player.getInventory().getItemInMainHand().setItemMeta(compassMeta);
                        player.sendMessage(ChatColor.BLUE + "Sua bússola foi atualizada!");
                    }
                    else if (player.getWorld() != speedrunner.getWorld()) {
                        player.sendMessage(ChatColor.BLUE + "Apontando para o último local que o jogador " + ChatColor.LIGHT_PURPLE + speedrunner + " foi visto!");
                    }
                }
            }
        }
    Can I get help?
     
  2. Here is the problem, PlayerInteractEvent fires even if the player is not holding an item. As you can see in the javadocs, getItem() can return null. So your compass.getType() is throwing the null pointer. So first add a not null check to compass. Or use the method provided in the event: right here

    P.S.

    Nested if statements are a big no-no, I highly suggest you refactor your code to look a bit cleaner

    Don't use Material.getMaterial() if you know you want a compass, just use "Material.COMPASS"
     
  3. Code (Java):
    if (speedrunner != null && event.hasItem())
    It's working now, tysm!
     
    • Winner Winner x 1