Unhandled exception executing command [Help me!!!!]

Discussion in 'Spigot Help' started by Womas_L, Jun 4, 2017.

  1. I am writing a heal plugin. But when i finished it, i got "Unhandled exception executing command" error.
    Here's the code
    Code (Text):
    package me.Womas_L.heal;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class heal extends JavaPlugin {
       
        public void onEnble(){
           
            getLogger().info("enble");
           
        }
       
        public void onDisable(){
           
            getLogger().info("disable");
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLable, String[] args){
                   
            Player target = Bukkit.getServer().getPlayer(args [0]); //target is player
           
            if(!(sender instanceof Player)){
               
                sender.sendMessage(ChatColor.RED + "Only player can use this command");
               
                return true;
               
            }
            Player player = (Player) sender;
           
            if(cmd.getName().equalsIgnoreCase("heal")){
           
            if(args.length == 0 ){
           
                player.setFoodLevel(20);
                player.setHealth(20);
                player.sendMessage(ChatColor.WHITE + "Healed" + ChatColor.GOLD + player.getName() + ".");
               
                return true;
                        }
            // if sender is console
           
            if(target == null){
               
                player.sendMessage(ChatColor.RED + "Could not find the player");
               
                return true;
            }
                           
            target.setFoodLevel(20);
            target.setHealth(20);
            target.sendMessage(ChatColor.GREEN + "You get healed.");
            player.sendMessage(ChatColor.WHITE + "Healed" + ChatColor.GOLD + target.getName());
            return true;
           
           
        }
            return true;      
        }

    }

    Code (Text):
    name: heal
    main: me.Womas_L.heal.heal
    version: 1.0
    author: Womas_L
    description: heal player

    commands:
      heal:
        usage: /<command>
        description: heal player

    Code (Text):
    [19:58:57 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'heal
    ' in plugin heal v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Spi
    got.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    1) ~[Spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServ
    er.java:650) ~[Spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerCo
    nnection.java:1354) [Spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.jav
    a:1189) [Spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.jav
    a:45) [Spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.jav
    a:1) [Spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:
    13) [Spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_131]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [Spigot.ja
    r:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:
    747) [Spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:
    399) [Spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:
    678) [Spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.jav
    a:576) [Spigot.jar:git-Spigot-3fb9445-6e3cec8]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.Womas_L.heal.heal.onCommand(heal.java:27) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Spi
    got.jar:git-Spigot-3fb9445-6e3cec8]
            ... 15 more
     
  2. 1: why use the bungee color import?
    2: did you register the command in on enable?
     
    • Agree Agree x 1
  3. Try this:
    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String commandLable, String[] args){
                 
         
      if(!(sender instanceof Player)){
          sender.sendMessage(ChatColor.RED + "Only player can use this command");
          return true;
      }
      Player player = (Player) sender;

      if(cmd.getName().equalsIgnoreCase("heal")){
            if(args.length == 0 ){
                player.setFoodLevel(20);
                player.setHealth(20);
                player.sendMessage(ChatColor.WHITE + "Healed" + ChatColor.GOLD + player.getName() + ".");
            } else { //If the leght of the argnuments is not 0
                Player target = Bukkit.getServer().getPlayer(args [0]); //target is player
                if(target == null){
                    player.sendMessage(ChatColor.RED + "Could not find the player");
                    return true;
                }
                target.setFoodLevel(20);
                target.setHealth(20);
                target.sendMessage(ChatColor.GREEN + "You get healed.");
                player.sendMessage(ChatColor.WHITE + "Healed" + ChatColor.GOLD + target.getName());
                return true;
            }
        }
            return true;    
    }
     
  4. Welp, thx a lot! Spare me. But i still not know the problem is, can you teach me?
     
  5. If the leght of the array (args) is 0, there is no args[0].
     
    • Like Like x 1