Solved Command Error

Discussion in 'Plugin Development' started by Gonmarte, Mar 13, 2016.

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

    Gonmarte

    Hello,
    Im working on a clans plugin and i was testing the commands and it throw me an error in the console.
    1-The lines 46,47,48 and 61,62,63 are with that yellow thing below the code i dont know why.
    2-When i tried to do /clan it throws me an error, and i think its something to do with that yellow thing.
    Code:
    package ClanCommands;
    
    import ClanSettings.Clans;
    import Main.Main;
    import SettingsMessages.MessageManager;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Commands implements CommandExecutor {
        Main main;
        MessageManager mm = new MessageManager();
    
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
            if (!((sender) instanceof Player)) {
                sender.sendMessage("So os players podem usar este comando!");
    
            } else {
                Player player = (Player) sender;
           
                if (cmd.getName().equalsIgnoreCase("clan")) {
                    if (args.length == 0) {
                        mm.info(player, "Clans command");
                        return true;
                    } else if (args.length == 1) {
    
                        if (args[0].equalsIgnoreCase("create")) {
                            if (args.length == 2) {
                                Clans.createClan(args[1], player);
                            }
                        } else if (args[0].equalsIgnoreCase("delete")) {
                            if (args.length == 2) {
                                Clans.deleteClan(player, Clans.getClan(player));
                            }
                        } else if (args[0].equalsIgnoreCase("add")) {
                            if (args.length == 2) {
                                Player target = Bukkit.getServer().getPlayer(args[1]);
                                if (args[1].equalsIgnoreCase(target.getName())) {
                           
                                    if (target == null) {
                                        mm.severe(player, "Player not found");
                                        return true;
                                } else {
                                        Clans.addPlayer(player, target, Clans.getClan(player));
                                        //MESSAGE
                                        return true;
                                    }
    
                                }
                            }
                        } else if (args[0].equalsIgnoreCase("remove")) {
                            if (args.length == 2) {
                                Player target = Bukkit.getServer().getPlayer(args[1]);
                                if (args[1].equalsIgnoreCase(target.getName())) {
                                    if (target == null) {
                                        mm.severe(player, "Player not found");
                                        return true;
                                    } else {
                                        Clans.removePlayer(player, target, Clans.getClan(player));
                                        //MESSAGE
                                        return true;
                                    }
    
                                }
                            }
                        } else if (args[0].equalsIgnoreCase("leave")) {
                            Clans.leaveClan(player, Clans.getClan(player));
    
                        } else if (args[0].equalsIgnoreCase("chat")) {
                            if (args.length == 2) {
                                if (args[1].equalsIgnoreCase("on")) {
                                    if (main.chatClan.contains(player.getName())) {
                                        mm.severe(player, "Ja tens o chat do clan ligado");
    
                                        return true;
                                    } else {
                                        main.chatClan.add(player.getName());
                                        mm.info(player, "Chat do clan ligado");
    
                                        return true;
                                    }
                                } else if (args[1].equalsIgnoreCase("off")) {
                                    if (main.chatClan.contains(player.getName())) {
                                        main.chatClan.remove(player.getName());
                                        mm.info(player, "saiste do chat co clan!");
    
                                        return true;
                                    } else {
                                        mm.info(player, "Nao podes sair, pk nao tas no chat co clan!");
    
                                        return true;
                                    }
                                }
                            }
                        } else if (args[0].equalsIgnoreCase("friendlyfire")) {
                            if (args.length == 2) {
                                if (args[1].equalsIgnoreCase("on")) {
                                    if (main.friendlyFireClan.contains(player.getName())) {
                                        mm.severe(player, "o Friendlyfire do teu clan ta ligado");
    
                                        return true;
                                    } else {
                                        main.friendlyFireClan.add(player.getName());
                                        mm.info(player, "ligaste o friendly fire");
    
                                        return true;
                                    }
                                } else if (args[1].equalsIgnoreCase("off")) {
                                    if (main.friendlyFireClan.contains(player.getName())) {
                                        main.friendlyFireClan.remove(player.getName());
                                        mm.info(player, "desligaste o friendly fire");
    
                                        return true;
                                    } else {
                                        mm.severe(player, "primeiro liga o e depois e que sais!");
    
                                        return true;
                                    }
                                }
                            }
                        }
                    }
    
                }
    
        }
    
            return false;
        }
    }
    
    Error in console:
    Code:
    18:48:26 [INFO] gonmarte issued server command: /clan
    18:48:26 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'clan
    ' in plugin Clans v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    9)
            at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(CraftServe
    r.java:523)
            at net.minecraft.server.v1_5_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:965)
            at net.minecraft.server.v1_5_R3.PlayerConnection.chat(PlayerConnection.j
    ava:883)
            at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java
    :840)
            at net.minecraft.server.v1_5_R3.Packet3Chat.handle(Packet3Chat.java:44)
            at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292
    )
            at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java
    :109)
            at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
            at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:5
    81)
            at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:2
    26)
            at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:4
    77)
            at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java
    :410)
            at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:5
    73)
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
            at ClanCommands.Commands.onCommand(Commands.java:26)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 15 more
    >
    
     
    Last edited: Mar 13, 2016
  2. Offline

    mine-care

    I think that is all you need to trouble shoot the issue. Also what is
    ??
     
    CodePlaysMinecraft likes this.
  3. Offline

    Zombie_Striker

    You are trying to get an object from an array where the index is greater than the size of the array.
    Fix this error by making sure the index you are getting is less than the length of the array.

    The reason you are getting the yellow lines are because you are first treating "target" as though it is not null, and then you test to make sure it is not null. If it is null, then what you are doing would through an NPE. Move the null check to before you do anything with target.
     
  4. Offline

    Gonmarte

    @mine-care yellow lines.

    So i need to put target check to before this ?
    Code:
    if (args[1].equalsIgnoreCase(target.getName())) {
    
    I dont unsderstand what you mean...
     
  5. Offline

    Zombie_Striker

    @Gonmarte
    Looking at your code again and looking at the error message, I noticed you must have edited your code after you got the error message. Line 26 of your current class only checks the command name and has nothing to do with arrays.

    Please post your updated class and updated error message.
    One thing to note though is this line. This the equivalent of saying "If there are one args and if there are two args, do blank" which will never be true because how can a number be both "1" and "2".
     
    Gonmarte likes this.
  6. Offline

    Gonmarte

    You are right '-' i put some supress warnings for mistake, the /clan command is working.
    As you were saying i tested the /clan create <name of clan> and it doesnt work.
    I understand what you said, but if i dont know the third argument [2] (<name of clan>)in the array once it starts with [0], how im going to check it?
    I dont know what is the clanname so i cant
    Code:
                        if (args[0].equalsIgnoreCase("create")) {
                            if (args.length == 2) {
                                String clanname;
                                if (args[1].equalsIgnoreCase(clanname)){
                                Clans.createClan(args[1], player);
                                }
                            }
                        }
    
     
    Last edited: Mar 13, 2016
  7. Offline

    Ligachamp

    This code does complete nonsense... Just check "if (!args[1].isNull())" before creating the clan ;)
     
  8. Offline

    Gonmarte

    Ik it doenst make sense, its an eg. When i arrive home i will try ty
     
  9. Offline

    mcdorli

    Please, don't try to teach things if you don't understand them yourself. IF YOU DO ARGS[1] AND THE ARRAY DOESN'T CONTAIN THAT MANY ELEMENTS, IT WILL THROW AN ARRAYOUTOFBOUNDSEXCEPTION, NO MATTER WHAT (Caps lock used to make it more obvious, I'm not shouting)
     
  10. Offline

    Zombie_Striker

    @mcdorli
    Pro-tip: Use Bold, Underline or Italics when you need to bring attention to certain parts.
     
    0566 likes this.
  11. Offline

    Gonmarte

  12. Offline

    Zombie_Striker

    if args.length is less than the index, then you know the object does not exist.
     
    0566 likes this.
  13. Offline

    mcdorli

    *If args.length is less than the index minus 1, then you know the object doesn't exist

    Bold and underlines are too mainstream
     
  14. Offline

    Ligachamp

    *Facepalm*

    Ok, I was explaining unclear...

    I was referring exactly to that ;) In his e.g. he did if (args.length == 2). Checking if args [1] is not null (which it couldn't really be) after that statement NEVER results in arrayoutofbound exceptions... :p But also it won't help him much further...
     
  15. Offline

    mcdorli

    It does, try it, in the moment you do args[1], before you check for .isNull(), it throws an ArrayOutOfBoundsException.
    This is how the code sees it:

    Code:
    if (args[1].isNull()) {...}
    
    Ok, let's break this up
    Code:
    if(
    args[1]
    .isNull()
    ) {...}
    
    What is args[1]?
    Code:
    Args array: ["Hello"]
    
    There's no args[1], let's throw an ArrayoutOfBoundsException

    Just so you remember, if there's no args[n], the compiler won't return a null, it immedialitely throws an ArrayOutOfBoundsException, this is not javascript, where you have a seemingly infinite array immedialitely with unknows on the empty spaces.

    And just because you like it...
    [​IMG]
    I love this picture
     
  16. Offline

    Gonmarte

    @Zombie_Striker @mcdorli Thank you :) I solved my problem. Sadly i got another one, but once its not a command error i will be creating another thread.
     
Thread Status:
Not open for further replies.

Share This Page