IndexOutOfBoundsException: Index: 1, Size: 1?

Discussion in 'Plugin Development' started by Epicballzy, Jul 25, 2014.

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

    Epicballzy

    So I picked up the tutorial How To Make a MiniGame and I went off and did my own thing. I have everything I want working, but when I try to remove a specific arena, I get an error on the console "IndexOutOfBoundsException: Index: 1, Size: 1". I've tried to do as much as I can on my own to fix it, but it still isn't working.

    This is the error:
    PHP:
    Caused byjava.lang.IndexOutOfBoundsExceptionIndex1Size1
            at java
    .util.ArrayList.rangeCheck(ArrayList.java:635) ~[?:1.7.0_45]
            
    at java.util.ArrayList.remove(ArrayList.java:474) ~[?:1.7.0_45]
            
    at net.mavenpvp.arenas.ArenaManager.removeArena(ArenaManager.java:137)
    Line 137 is:
    Code:java
    1. list.remove(i);

    Pretty much used to remove the arenas id.

    But for some reason it gives an error.

    Here's some more code if its needed:
    Code:java
    1. public void removeArena(int i) {
    2. Arena a = getArena(i);
    3. if(a == null) {
    4. return;
    5. }
    6. arenas.remove(a);
    7.  
    8. Files.Arenas.set("Arenas." + i, null);
    9. List<Integer> list = Files.Arenas.getIntegerList("Arenas.Arenas");
    10. list.remove(i);
    11. Files.Arenas.set("Arenas.Arenas", list);
    12. Files.saveArenas();
    13. }


    To remove the arena:
    Code:java
    1. if(args[0].equalsIgnoreCase("remove")) {
    2. if(player.hasPermission("mavenarenas.arena.remove")) {
    3. if(args.length != 2){
    4. player.sendMessage("Insuffcient arguments!");
    5. return true;
    6. }
    7. int num = 0;
    8. try{
    9. num = Integer.parseInt(args[1]);
    10. player.sendMessage("Invalid arena ID");
    11. }
    12. ArenaManager.getManager().removeArena(num);
    13.  
    14. return true;
    15. } else {
    16. MessageManager.getInstance().msg(player, ChatColor.RED + "You don't have permission!");
    17. return true;
    18. }
    19. }
     
  2. Offline

    fireblast709

    Epicballzy arrays (and thus Lists, since they are based on arrays) start with index 0, not 1.

    Thus all valid indices are between 0 and size - 1, which leaves 0 as the only option.
     
  3. Offline

    Epicballzy

    fireblast709 So what do you suggest would be the best way to fix this? Would there be some sort of check I would have to do?
     
  4. Offline

    Lazertx

    Just change line 9 to
    Code:java
    1. num = Integer.parseInt(args[1]) - 1;

    and that should fix the issue because arrays start from 0 not 1 like fireblast said.
     
  5. Offline

    Necrodoom

  6. Another thing you need to watch out for is that you're at risk of having a NumberFormatException by trying to parse the arg to an integer without making sure the string is actually a valid number. As it stands, your code assumes that the player will always enter the correct input which is a very dangerous thing to assume. Your best option is probably to surround the parsing with try/catch brackets so you can properly handle the exception.

    Code:java
    1. try
    2. {
    3. num = Integer.parseInt(args[1]) - 1;
    4. {
    5. //Do something here like tell the player their input was wrong
    6. }
     
Thread Status:
Not open for further replies.

Share This Page