Solved Array index out of bound exception: 1

Discussion in 'Plugin Development' started by Twisted_Panda, Aug 16, 2013.

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

    Twisted_Panda

    So, I keep getting this pesky issue.

    The code:
    Code:
            else if(args[0].equalsIgnoreCase("sethq")) {
                if(Main.pvp.getConfig().getString("Players." + p.getName()) != null) {
                    if(getTeamManagers(p).contains(p.getName())) {
                        Main.pvp.getConfig().set("Teams." + Main.pvp.getConfig().getString("Players." + p.getName()) + ".HQ.X", p.getLocation().getX());
                        Main.pvp.getConfig().set("Teams." + Main.pvp.getConfig().getString("Players." + p.getName()) + ".HQ.Y", p.getLocation().getY());
                        Main.pvp.getConfig().set("Teams." + Main.pvp.getConfig().getString("Players." + p.getName()) + ".HQ.Z", p.getLocation().getZ());
                        Main.pvp.getConfig().set("Teams." + Main.pvp.getConfig().getString("Players." + p.getName()) + ".HQ.World", p.getWorld().getName());
                        Main.pvp.getConfig().options().copyDefaults(true);
                        Main.pvp.saveConfig();
                        Main.pvp.saveConfig();
                        for(Player pl : Bukkit.getOnlinePlayers()) {
                            if(Main.pvp.getConfig().getString("Teams." + args[1] + ".Members").contains(pl.getName())) {
                                pl.sendMessage(ChatColor.GRAY + "Team Headquarters has been updated by " + p.getName() + ".");
                            }
                        }
                    } else {
                        p.sendMessage(ChatColor.RED + "You are not a manager of this team!");
                    }
                } else {
                    p.sendMessage(ChatColor.RED + "You are not on a team!");
                }
            }
            return false;
        }
    }
    I am getting 0 errors in java, any help?
     
  2. Offline

    hatstand

    Your args[] array isn't as long as you think it is.

    To clarify, you're accessing args[1] when it doesn't exist. Check that the arguments are the length you need, don't assume they are.
     
  3. Offline

    Twisted_Panda

    I dont get what you mean?
     
  4. Offline

    Axe2760

    Twisted_Panda basically there are not enough args. Check if args.length is long enough before calling args[1].
     
  5. Offline

    Twisted_Panda

    My brain is frozen, atm I dont seem to understand :eek:
     
  6. Offline

    Axe2760

    Twisted_Panda lol

    Someone types in a command, yes?
    Code:
    /test set derpvariable 10
    
    You get the args:
    Code:
     'set', 'derpvariable', '10'
    You have three args, correct?
    Code:
    args[0] == "set"
    args[1] == "derpvariable"
    args[2] == "10"
    
    You probably know all that, yeah?

    But what you're doing wrong, is using args[1] before you know it exists. You don't know if someone simply typed in /mycommand sethq. If they did, args[1] doesn't exist. The only one that exists is args[0], because they only typed one word after the command.

    So to fix that, you need to check to see how many arguments/words were typed after the command.
    Code:
    String teststring = "4:5:3:8";
    String[] args = teststring.split(":");
    if (args.length >= 2){
      String test = args[0] + "." + args[1];
    }
    
    Consider the above code. What if args.length was less than 2? Then there is no args[1], there is only args[0]. Which will throw the illegalargumentexception, the same thing that you are getting!
    args is really:
    Code:
    '4', '5', '3', '8'
    
    But say it was:
    Code:
    '3'
    
    If you tried to get the second number, it would throw the error as there is no second number!

    Hope this helps! :D
     
    Twisted_Panda, artish1 and drtshock like this.
  7. Offline

    artish1

    Wow that is the most noob friendly Post i have ever seen. It's so well explained i think i'm going to cry.
     
  8. Offline

    Twisted_Panda

    I got it working thanks for that tutorial <3
     
  9. Offline

    Axe2760

Thread Status:
Not open for further replies.

Share This Page