null org.bukkit.command.CommandException: Unhandled exception executing command 'ping' in plugin Cor

Discussion in 'Plugin Development' started by Zuedy, May 7, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    Zuedy

    Hello, I'm trying to make a simple /ping command.
    If I /ping <target>, it works. But when I /ping <randomname> it gives me an error in chat + console.
    "An internal error occurred while attempting to perform this command."
    Code:
    import org.bukkit.command.Command;import org.bukkit.command.CommandExecutor;import org.bukkit.command.CommandSender;import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;import org.bukkit.entity.Player;public class PingCommand implements CommandExecutor {
    
    private final Main main; public PingCommand(Main main) {
    this.main = main;}
    
    @Overridepublic boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
    
    if (sender instanceof Player) {
    Player player = (Player) sender; int ping = ((CraftPlayer) player).getHandle().ping; if(args.length == 0) {
    player.sendMessage("You ping is currently " + ping + "ms.");}
    if(args.length == 1) {
    Player target = Bukkit.getPlayerExact(args[0]); int pingTarget = ((CraftPlayer) target).getHandle().ping; if (target != null) {
    player.sendMessage(target.getDisplayName() + "'s ping is currently " + pingTarget + "ms.");} else{
    player.sendMessage("That players does not exist.");}
    }
    if (args.length >= 2) {
    player.sendMessage("Wrong usage.");}
    }
    
    
    return false;}
    }
    That is my code ^^
    (I have tried many things I found online, but didn't find any solutions...)

    My Error in Console
    Code:
    [21:57:13 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ping' in plugin Core v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[server.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [server.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0-262]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0-262]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [server.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [server.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Thread.java:748) [?:1.8.0-262]
    Caused by: java.lang.NullPointerException
            at dev.zuedy.core.commands.PingCommand.onCommand(PingCommand.java:30) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[server.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
     
    Last edited by a moderator: May 7, 2021
  2. Offline

    Kars

    @Zuedy which line is line 30?
     
  3. Offline

    Strahan

    You are checking if target is null, but only after you've already tried to use it. Obviously, that check needs to be before you attempt to use it with anything.

    PS also you could optimize that a bit. There is no need to specifically deny the console access to the command, the only place you need a Player is if they want to check their own ping. Also you had a few English errors. Here is how I would refactor that:
    Code:
    @Override
    public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
      switch (args.length) {
      case 0:
        if (!(sender instanceof Player)) {
          sender.sendMessage("You cannot check your own ping if you are console!");
          return true;
        }
        Player player = (Player) sender;
        int ping = ((CraftPlayer) player).getHandle().ping;
        player.sendMessage("Your ping is currently " + ping + "ms.");
        break;
     
      case 1:
        Player target = Bukkit.getPlayerExact(args[0]);
        if (target == null) {
          player.sendMessage("That player does not exist.");
          return true;
        }
     
        int pingTarget = ((CraftPlayer) target).getHandle().ping;
        player.sendMessage(target.getDisplayName() + "'s ping is currently " + pingTarget + "ms.");
        break;
     
      default:
        player.sendMessage("Wrong usage.");
        break;
      }
      return true;
    }
     
    Last edited: May 8, 2021
  4. Offline

    Zuedy

    I needed to change "player.sendMessage..." to "sender.sendMessage..." because I had some errors.
    Thank you for correcting me on my English mistake, sorry for that.
    It works perfectly now, without any errors.
    Thanks!
     
  5. Offline

    Strahan

    Yea, sorry, looking back on it I see I forgot to change those references when I moved the Player instantiation to just the one part, oops! Glad you caught and fixed it. No prob :)
     
Thread Status:
Not open for further replies.

Share This Page