Solved How to fix org.bukkit.command.CommandException?

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

  1. When I try to use the command it keeps giving this error:
    Code (Text):
    [10:22:25] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'hunt' in plugin Manhunt v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
        at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:810) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
        at net.minecraft.server.v1_16_R3.PlayerConnection.handleCommand(PlayerConnection.java:2238) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
        at net.minecraft.server.v1_16_R3.PlayerConnection.c(PlayerConnection.java:2053) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
        at net.minecraft.server.v1_16_R3.PlayerConnection.a(PlayerConnection.java:2006) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
        at net.minecraft.server.v1_16_R3.PacketPlayInChat.a(PacketPlayInChat.java:49) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
        at net.minecraft.server.v1_16_R3.PacketPlayInChat.a(PacketPlayInChat.java:7) ~[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]
    Caused by: java.lang.NullPointerException
        at dev.diaburo.manhunt.Manhunt.onCommand(Manhunt.java:53) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.16.5.jar:git-Tuinity-"94f025a"]
    Code (Java):
    [Line 48] Player target = Bukkit.getPlayerExact(args[0]);
                        if (target instanceof Player) {
                            ItemStack compass = new ItemStack(Material.COMPASS);
                            CompassMeta compassMeta = (CompassMeta) compass.getItemMeta();
                            compassMeta.setLodestoneTracked(false);
                            compassMeta.setLodestone(speedrunner.getLocation());
                            compassMeta.setDisplayName(ChatColor.BLUE + speedrunner.getDisplayName() + "tracker");
                            compass.setItemMeta(compassMeta);
                            player.getInventory().addItem(compass);

                            player.sendMessage("You are now hunting " + ChatColor.BLUE + target.getDisplayName());
                            player.sendMessage("Right click the compass to track the player!");
                            speedrunner = target;
                        }
                        else {
                            player.sendMessage(ChatColor.RED + "Player not found");
                        }
     
  2. Since you did not provide a lot of info it's hard to say exactly what the problem is, at this point I would say that maybe the Bukkit.getPlayerExact() method is returning null, add a null check before you start messing with it and it should fix the problem
     
  3. I think speedRuner is null in here: compassMeta.setLodestone(speedrunner.getLocation());.
     
    • Agree Agree x 1
  4. target instanceof Player acts as a null-check in this case - instanceof will return false if target is null. I'm not sure why they're using instanceof over a null-check, but that part works, /shrug.

    The first line of op's code mentioned in the stacktrace is Manhunt line 53, which seems to be the line that MatiRosen mentioned.
    @Diaburo show us where you try to set 'speedRunner' to a value.
     
    • Agree Agree x 1
  5. Looked at the stack trace again and you're right. Line 53 is shown here
    Code (Text):
    at dev.diaburo.manhunt.Manhunt.onCommand(Manhunt.java:53) ~[?:?]
    So yeah, speedrunner is null

    (Also I didn't know that the instanceof keyword would return false of the provided value was null, huh, the more you know)
     
  6. Lmao how I didn't saw that, thanks everyone, it's working now
     
    • Friendly Friendly x 1