Solved IndexOutOfBounds error help

Discussion in 'Plugin Development' started by PatoTheBest, Nov 27, 2014.

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

    PatoTheBest

    I'm getting IndexOutOfBoundsException, this is my code
    Code:java
    1. private void rollbackDelay(int i) {
    2. setArenaStatus(Status.RESTARTING);
    3. int i2 = i;
    4. int blocksToRollback = 16*16*arena.getWorld().getMaxHeight()*8;
    5. for (int i1 = i; i1 < blocksToRollback + i; i1++) {
    6. if(states.get(i1) != null) {
    7. BlockState state = states.get(i1);
    8. state.update(true);
    9. i2++;
    10. } else { return; }
    11. }
    12. final int i3 = i2;
    13. System.out.println(i3);
    14. Bukkit.getScheduler().runTaskLater(TheTowers.main, new BukkitRunnable() {
    15. public void run() {
    16. rollbackDelay(i3);
    17. }
    18. }, 5L);
    19. }

    The stackTrace
    Code:
    [18:30:43] [Server thread/INFO]: [0;33;1m[0;35;1mRollbacking arenaOriginal[m
    [18:30:43] [Server thread/INFO]: 524288
    [18:30:45] [Server thread/INFO]: 1048576
    [18:30:48] [Server thread/INFO]: 1572864
    [18:30:48] [Server thread/INFO]: 2097152
    [18:30:48] [Server thread/WARN]: [TheTowers] Task #37 for TheTowers v1.0.3 generated an exception
    java.lang.IndexOutOfBoundsException: Index: 2294496, Size: 2294496
        at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.7.0_71]
        at java.util.ArrayList.get(Unknown Source) ~[?:1.7.0_71]
        at com.minebone.thetowers.util.Arena.rollbackDelay(Arena.java:320) ~[?:?]
        at com.minebone.thetowers.util.Arena.access$0(Arena.java:315) ~[?:?]
        at com.minebone.thetowers.util.Arena$1.run(Arena.java:346) ~[?:?]
        at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftTask.run(CraftTask.java:71) ~[thetowers.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at org.bukkit.craftbukkit.v1_7_R4.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) [thetowers.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:641) [thetowers.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [thetowers.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [thetowers.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [thetowers.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [thetowers.jar:git-Spigot-1.7.9-R0.2-208-ge0f2e95]
    [18:36:23] [Server thread/INFO]: CONSOLE: Stopping the server..[m
     
  2. Offline

    ZanderMan9

    I don't see what could be causing it myself. Perhaps someone else can shed some light on it.
     
  3. Offline

    Tecno_Wizard

    PatoTheBest, Please put a comment on the line that is causing it, as I cannot determine it without the entire class.
     
  4. Offline

    PatoTheBest

  5. Offline

    Tecno_Wizard

    PatoTheBest
    Uhhhhhhhh...... LOL. Give me a few min, I am seeing something very strange in your index, it's the exact max on java's integer class.

    PatoTheBest I believe the error is actually originating further up the trace.
    Can you post this? com.minebone.thetowers.util.Arena.access$0(Arena.java:315) ~[?:?]

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  6. Offline

    PatoTheBest

    See the picture, plus I searched and Integer.MAX_VALUE is equal to 2,147,483,647.
     
  7. Offline

    Tecno_Wizard

    I know, I realized that later. Can you show me the line that calls that method though? I believe that the value you are passing it is an ArrayList call that is spazing out.
     
  8. Offline

    PatoTheBest

  9. Offline

    Tecno_Wizard

    PatoTheBest
    Found it. Lie 320 iterates until it reaches the max of the arraylist and throws an outofbounds because it is trying to get a value that does not exist. A null check won't work, you need a try catch.
     
    PatoTheBest likes this.
  10. Offline

    PatoTheBest

    Thanks! I fixed it finally! Thanks for the help
     
  11. Offline

    Rocoty

    Tecno_Wizard IndexOutOfBoundsExceptions aren't meant to be caught. They are meant to be avoided in the first place. A quick glance at the code and one should be able to easily determine that the problem lies in the for loop declaration itself, namely the +1.
     
    ROTN likes this.
  12. Offline

    Tecno_Wizard

    I am aware of that, but I wanted to show him how the code he had for that line was not doing its intended purpose. Yes, it is always better to fix the loop.
    PatoTheBest, mark as solved
     
Thread Status:
Not open for further replies.

Share This Page