ArrayIndexOutOfBoundsException -1 ??

Discussion in 'Plugin Development' started by haussik, Apr 1, 2015.

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

    haussik

    Here's my code:
    Code:
        private List<Integer> amounts = new ArrayList<Integer>();
        private List<String> targets = new ArrayList<String>();
      
        //              [0]    [1]
        //        /mise 100 chargon
      
        @SuppressWarnings("unused")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if(cmd.getName().equalsIgnoreCase("mise"))
        {
            if(sender instanceof Player)
            {
                Player player = (Player) sender;
                if(args.length == 2)
                {
                    int amount = Integer.parseInt(args[0]);
                    amounts.add(amount);
                  
                    String target = args[1];
                    targets.add(target);
                  
                    if(amounts.size() != targets.size())
                    {
                        sender.sendMessage(ChatColor.RED + "error");
                    }
                  
                    int idTargets = targets.indexOf(args[1]);
                    int idAmounts = amounts.indexOf(idTargets);
                  
                    sender.sendMessage(ChatColor.RED + "working: " + targets.get(idTargets) + " for " + targets.get(idAmounts));
                  
                    return true;
                }else{
                    sender.sendMessage(ChatColor.RED + "error");
                    return false;
                }
            }
            return true;
        }
        return true;
        }
    Recapitulation of the plugin: I put a amount to a player (like gta, and the player index is the same as the amount index in the array list. If not, error) and later, i will add a reward system.

    But when i do it : /mise 100 chargon, the console throws my an error and i can't deal with it :( Please help, thanks.
     
    Last edited: Apr 1, 2015
  2. Offline

    nverdier

    @haussik Please put the latest.log (found in the logs folder) in a pastebin.
     
  3. Offline

    haussik

  4. Offline

    nverdier

    @haussik Could you please post the entire latest.log?
     
  5. Offline

    haussik

    @nverdier Sorry i did a lot of /reload ^^ There it is,

    http://pastebin.com/iCp0JNGq

    EDIT: i did some research and it's the amounts index that cause the bug (i want the same index for the amounts and the targets)
     
  6. Offline

    nverdier

    @haussik First of all, read this. Next, what's on line 54? Derived from at
    Code:
    com.lmoh.commands.Mise.onCommand(Mise.java:54) ~[?:?]
     
  7. Offline

    haussik

    @nverdier

    Line 54:
    Code:
    sender.sendMessage(ChatColor.RED + "Working: " + targets.get(idTargets) + " for " + targets.get(idAmounts));
    with some debug, error at: targets.get(idAmounts)
     
  8. Offline

    Lolmewn

    Instead of using two lists, I would suggest you use a HashMap<String, Integer>, or if you want multiple ints per target, either a MultiMap or a Map<String, List<Integer>>.
     
  9. Offline

    haussik

    @Lolmewn I'm starting now, hashmap and multimap are for later :p Thanks anyway

    Fixed!

    replace
    Code:
    int idAmounts = amounts.indexOf(idTargets);
    by
    Code:
    int idAmounts = 0;
                    for(int i = 0; i < idTargets; i++)
                    {
                        idAmounts = i;
                    }
    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Apr 1, 2015
Thread Status:
Not open for further replies.

Share This Page