title: IndexOutOfBoundsError while coding a Game in Java
body: <p>I ran into a IndexOutOfBound error while coding a school project. This problem isn't stable, this problem seems to occur very random... </p>
  
  <pre><code>public void run(){ // Main game methode 
      Image Background = this.images.get(0);//get background because we can
      Player Player1 = this.players.get(0);//get player 1
      Player Player2 = this.players.get(1);//get player 2
  
      Player1.setHorSp(1);//give start speed to player 1
      Player2.setHorSp(1);//give start speed to player 2
      while(this.running == true &amp;&amp; this.crash == false){ // main game loop, loops when game is started and there is no crash
          // Player 1
          if(this.turnable == 0 &amp;&amp; Player1.getDir() &gt; 0){
              Player1.setPrevLine(Player1.getCurrentLine());//safe previous line for testing
              if(Player1.getDir() == 1){ //check which key and horizontal speed so it can't move in opposite direction
                  if(Player1.getHorSp() == -1){
                      Player1.setLineX(Player1.getX()+grid/2); //set end of line
                      Player1.setLineY(Player1.getY()+grid/2);
                      addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                      Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                      Player1.setX(Player1.getX());
                      Player1.setY(Player1.getY()-Player1.getWidth()+Player1.getHeight());
                  }
                  if(Player1.getHorSp() == 1){
                      Player1.setLineX(Player1.getX()+Player1.getWidth()-grid/2); //set end of line
                      Player1.setLineY(Player1.getY()+grid/2);
                      addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                      Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                      Player1.setX(Player1.getX()-Player1.getHeight()+Player1.getWidth());
                      Player1.setY(Player1.getY()+Player1.getHeight()-Player1.getWidth());
                  }
                  Player1.setLineNr(Player1.getLineNr()+1); //tell player that it had turned
                  Player1.setImage("BikeBlueUp.png"); //change image in the right direction
                  Player1.setWidth(grid); //correct images size
                  Player1.setHeight(2*grid);
                  Player1.setLineX(Player1.getX()+grid/2);//set point line is atteched to bibe
                  Player1.setLineY(Player1.getY()+grid/2);
                  Player1.setHorSp(0); //give speed in the right direction
                  Player1.setVerSp(-1);
              }
              if(Player1.getDir() == 2){ //check which key and if is in grid
                  if(Player1.getVerSp() == -1){
                      Player1.setLineX(Player1.getX()+grid/2); //set end of line
                      Player1.setLineY(Player1.getY()+grid/2);
                      addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                      Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                      Player1.setX(Player1.getX());
                      Player1.setY(Player1.getY());
                  }
                  if(Player1.getVerSp() == 1){
                      Player1.setLineX(Player1.getX()+grid/2); //set end of line
                      Player1.setLineY(Player1.getY()+Player1.getHeight()-grid/2);
                      addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                      Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                      Player1.setX(Player1.getX());
                      Player1.setY(Player1.getY()+Player1.getHeight()-Player1.getWidth());
                  }
                  Player1.setLineNr(Player1.getLineNr()+1); //tell player that it had turned
                  Player1.setImage("BikeBlueRight.png"); //change image in the right direction
                  Player1.setWidth(2*grid); //correct images size
                  Player1.setHeight(grid);
                  Player1.setLineX(Player1.getX()+Player1.getWidth()-grid/2);//set point line is atteched to bibe
                  Player1.setLineY(Player1.getY()+grid/2);
                  Player1.setHorSp(1); //give speed in the right direction
                  Player1.setVerSp(0);
              }
              if(Player1.getDir() == 3){ //check which key and horizontal speed so it can't move in opposite direction
                  if(Player1.getHorSp() == -1){
                      Player1.setLineX(Player1.getX()+grid/2); //set end of line
                      Player1.setLineY(Player1.getY()+grid/2);
                      addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                      Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                      Player1.setX(Player1.getX());
                      Player1.setY(Player1.getY());
                  }
                  if(Player1.getHorSp() == 1){
                      Player1.setLineX(Player1.getX()+Player1.getWidth()-grid/2); //set end of line
                      Player1.setLineY(Player1.getY()+grid/2);
                      addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                      Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                      Player1.setX(Player1.getX()-Player1.getHeight()+Player1.getWidth());
                      Player1.setY(Player1.getY());
                  }
                  Player1.setLineNr(Player1.getLineNr()+1); //tell player that it had turned
                  Player1.setImage("BikeBlueDown.png"); //change image in the right direction
                  Player1.setWidth(grid); //correct images size
                  Player1.setHeight(2*grid);
                  Player1.setLineX(Player1.getX()+grid/2);//set point line is atteched to bibe
                  Player1.setLineY(Player1.getY()+Player1.getHeight()-grid/2);
                  Player1.setHorSp(0); //give speed in the right direction
                  Player1.setVerSp(1);
              }
              if(Player1.getDir() == 4){ //check which key and horizontal speed so it can't move in opposite direction
                  if(Player1.getVerSp() == -1){
                      Player1.setLineX(Player1.getX()+grid/2); //set end of line
                      Player1.setLineY(Player1.getY()+grid/2);
                      addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                      Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                      Player1.setX(Player1.getX()-Player1.getHeight()+Player1.getWidth());
                      Player1.setY(Player1.getY());
                  }
                  if(Player1.getVerSp() == 1){
                      Player1.setLineX(Player1.getX()+grid/2); //set end of line
                      Player1.setLineY(Player1.getY()+Player1.getHeight()-grid/2);
                      addLine(Player1.getLineX(),Player1.getLineY(),Player1.getLineX(),Player1.getLineY(),Color.BLUE);//make next line
                      Player1.setCurrentLine(Player1.getLineNr()+Player2.getLineNr());//use next line
                      Player1.setX(Player1.getX()-Player1.getHeight()+Player1.getWidth());
                      Player1.setY(Player1.getY()+Player1.getHeight()-Player1.getWidth());
                  }
                  Player1.setLineNr(Player1.getLineNr()+1); //tell player that it had turned
                  Player1.setImage("BikeBlueLeft.png"); //change image in the right direction
                  Player1.setWidth(2*grid); //correct images size
                  Player1.setHeight(grid);
                  Player1.setLineX(Player1.getX()+grid/2);//set point line is atteched to bibe
                  Player1.setLineY(Player1.getY()+grid/2);
                  Player1.setHorSp(-1); //give speed in the right direction
                  Player1.setVerSp(0);
              }
          }
  </code></pre>
  
  <p>this generates the error: </p>
  
  <pre><code>Exception in thread "Thread-5" java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
  at java.util.ArrayList.RangeCheck(ArrayList.java:547)
  at java.util.ArrayList.get(ArrayList.java:322)
  at Game.run(Game.java:388)
  at java.lang.Thread.run(Thread.java:619)
  </code></pre>
  
  <p>still this error now occured while pressing the up button. This error didn't occur for the other 2 times and then out of nowwere it happens.</p>
  
  <p>this error was generated after pressing the right button</p>
  
  <pre><code>Exception in thread "Thread-5" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11
  at java.util.ArrayList.RangeCheck(ArrayList.java:547)
  at java.util.ArrayList.get(ArrayList.java:322)
  at Game.run(Game.java:388)
  at java.lang.Thread.run(Thread.java:619)
  </code></pre>
  
  <p>this error was generated after pressing the right button, and it worked, in the same game, for 1 time earlier... And even after 3 day's of debugging we still can't see a patron. </p>
  
  <p><em>Has anyone ever seen this before and if so: how do we fix it?</em></p>
  
  <p>--JYT</p>
  
  <p>Ps. the whole code is here <a href="http://pastebin.com/0wvyr3qh" rel="nofollow">http://pastebin.com/0wvyr3qh</a></p>