Solved No args error. Help.

Discussion in 'Spigot Plugin Development' started by xAndrea100, Feb 10, 2019.

  1. Hi, I'm trying to make a plugin. I'm adding a command, but when I try this, it give me an error:

    Code (Text):
    [22:51:36 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'prova' in plugin FirstPlugin v1.0-SNAPSHOT
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_201]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_201]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_201]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at jarany.firstplugin.Comandi.onCommand(Comandi.java:23) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
    This is my code:
    Code (Text):
    public class Comandi implements Listener, CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            if(args[0].equalsIgnoreCase("1")){
                sender.sendMessage("prova1");
            }
            else if(args[0].equalsIgnoreCase("2")){
                sender.sendMessage("prova2");
            }
            else if(args[0].equalsIgnoreCase(null)){
                    sender.sendMessage("provaaa");
            }
            return true;
        }
    }
    I tried to change args[0].equalsIgnoreCase(null) with args[0] = null and with args.length == 0 , but it give me the same error.

    Can you help me?
    Thanks
     
  2. md_5

    Administrator Developer

    Post your code with that - it is the correct approach, but maybe you did something else wrong.
    It needs to go FIRST so you don't accidentally access and args[0] that doesn't exist.
    That will never be true for reasons stated above.
     
    • Like Like x 1
    • Agree Agree x 1
  3. By the way, why are you implementing a Listener in a command class. You should only implement CommandExecutor and create another class to listen to events stuff.
     
    • Like Like x 1
  4. Use the switch block instead of else if else if else if...
     
    • Like Like x 1
  5. Dal codice ho visto che sei italiano, poiché ho notato che nei sendMessage hai inserito "prova".
    Comunque l'errore potrebbe essere dato dal fatto che nel comando non hai inserito l'argomento e di conseguenza da questo errore. Per verificare se hai messo l'argomento ( args[0] ) devi controllare stesso la lunghezza degli argomenti, e verificare se l'argomento 0 esiste. Per farlo si scrive così di seguito:

    Code (Java):
    if(args.length == 1){
    //TODO codice
    }
    Praticamente un esempio, come quello che hai fatto tu, è il seguente:

    Code (Java):
    if(args.length ==1) {
                    switch(args[0]) {
                    case "1":
                        sender.sendMessage("prova1");
                        break;
                    case "2":
                        sender.sendMessage("prova2");
                        break;
                    default:
                        sender.sendMessage("prova1");
                        break;
                    }
                }
    Altrimenti:

    Code (Java):
    if(args.length ==1) {
                    if(args[0].equalsIgnoreCase("1")) {
                        sender.sendMessage("prova1");
                    } else if(args[0].equalsIgnoreCase("2")) {
                        sender.sendMessage("prova2");
                    }
                }
    Spero d'esserti stato d'aiuto :)
     
    • Agree Agree x 1
  6. Strahan

    Benefactor

    I have no idea what you are saying there, but I'd recommend a slight change.
    Code (Text):
    if (args.length < 1) {
      // some sort of feedback explaining how to use cmd
      return true
    }
    switch(args[0]) {
    case "1":
        sender.sendMessage("prova1");
        break;
    case "2":
        sender.sendMessage("prova2");
        break;
    default:
        sender.sendMessage("prova1");
        break;
    }
    Less annoying indentation and more user friendly.
     
    • Like Like x 1
  7. I solved the problem thanks to you!
    Now my code is:
    Code (Text):
    public class Comandi implements Listener, CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

            if(args.length == 1) {
                switch(args[0]) {
                    case "1":
                        sender.sendMessage("prova1");
                        break;
                    case "2":
                        sender.sendMessage("prova2");
                        break;
                    default:
                        sender.sendMessage("prova1");
                        break;
                }
            }
            else if(args.length == 0){
                sender.sendMessage("provaaa");
            }
            return true;
        }
    }
     
    @allen_zhang I implemented a Listener because I want to add an event!
    Thanks @md_5 @allen_zhang @Il_totore @de_cifris (Si sono italiano :D) and @Strahan !
    If you want give me other suggestions!
    Bye!
     
    • Like Like x 1
  8. Please mark this thread as solved.
     
  9.  
    • Like Like x 1
  10. Strahan

    Benefactor

    It's best to separate your functionality into their own classes. Like a class for the command and a separate class for the listener. I mean, I suppose it's all personal preference as you could just lump it all into a single class (which I did when I started out) but I think in the end for clarity's sake individual classes are better.
     
    • Like Like x 1

Share This Page