3 Replies Latest reply: Feb 3, 2010 10:59 PM by 843789 RSS

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

    843789
      I have this program that acts like a battle from Lord Of the Rings. I set the size on the board then Randomize Elves,Orcs, and moutains. When the user selects 1 to start the battle it crashes. All the Elves and Orcs are set to move up,down,left,right 1 space at a time. If and Elv or Orc hits a mountain they dont move or if they hit a teammate they stay in there current position. If an Orc and Elve move onto themselves then a battle starts. the battle depends on a random strength set from 0-10 if the strength is the same it is a draw.

      One problem is that when an Elve or an Orc move they Duplicate (Example- Move left
      E (then move left)
      E E (It dident remove itself)
      ....E (It should move and remove itself from start position)

      public class Battlefield {
      
      private int numElves;
      private int numOrcs;
      private int row; 
      private int col; 
      private Square battlefield[][];
      
      public Battlefield(){
      
      }
      public Battlefield (int row, int col){
      this.row = row;
      this.col = col;
      battlefield = new Square [row][col];
      for (int i = 0; i < row; i++)
      for (int j = 0; j < col; j++)
      battlefield[i][j] = new Square();
      
      }
      
      
      
      
      public int getRow(){return row;
      
      }
      
      public int getCol(){return col;
      
      }
      
      public Square getSquare(int r, int c){//return Square(0, 0);
      
      return battlefield[r][c];
      
      }
      
      public int getNumOrcs(){return numOrcs;
      
      }
      
      public int getNumElves(){return numElves;
      
      }
      
      
      public String toString(){
      return "Row:"+row+ "Col:"+col;+
      +}+
      
      +public void setRow(int row){+
      +this.row = row;+
      +}+
      
      +public void setCol(int col){+
      +this.col = col;+
      +}+
      
      +public void setSquare(int row,int col, Square square){+
      +battlefield[row][col] = new Square (square);+
      
      +}+
      +public void display ( ) {+
      +// display top row of ---+
      +for (int i = 0; i < col; i++)
      System.out.print (" -");
      System.out.println();
      for (int i = 0; i < row; i++) {
      System.out.print("|");
      
      for (int j = 0; j < col; j++) 
      System.out.print(battlefield[i][j].toString()) ;
      System.out.println("|");
      
      }
      System.out.println();
      for (int i = 0; i < col+2; i++)
      System.out.print (" -");
      
      }
      
      
      public void moveActor(int direction,int row,int col){
      / *int newRow =row +1;+*
      *+int newCol =col +1;+*
      *+*if (direction == 0){*+*
      *+*newRow = row +1;*+*
      *+newCol = col +0;}+*
      *+*if (direction == 1){*+*
      *+*newRow = row - 1;*+*
      *+*newCol = col +0;}*+*
      *+if (direction == 2){+*
      *+newRow = row +0;+*
      *+*newCol = col - 1;}*+*
      *+*if (direction == 3){*+*
      *+*newRow = row +0;*+*
      *+newCol = col +1;}/+
      *+if (direction == 0){+*
      *+newRow = row - 1;+*
      *+newCol = col;}+*
      *+if (direction == 1){+*
      *+newRow = row +1;+*
      +newCol = col;}+
      +if (direction == 2){+
      +newRow = row+ 0;
      newCol = col +1;}+
      +if (direction == 3){+
      +newRow = row+ 0;
      newCol = col - 1;}
      
      if (battlefield[row][col].getTerrain()=='m')
      return;
      
      if (battlefield[row][col].getActor().getType()==' ')
      return;
      
      // if ((battlefield[newRow][newCol].getActor().getType()=='e')
      // battlefield[row][col].getActor().getType()== " ")
      // return;
      
      if (direction ==0&&row==0)return;
      else if (direction ==1&&row==this.row-1)return;
      else if (direction ==2&&col==0)return;
      else if (direction ==3&&col==this.col-1)return;
      else {;}
      //else if (battlefield[newRow][newCol].getTerrain() == 'm') return; 
      
      if (battlefield[row][col].getActor().getType()==(battlefield[newRow][newCol].getActor().getType()))
      return;
      
      
      
      if (battlefield[row][col].getActor().getStrength()>battlefield[newRow][newCol].getActor().getStrength())
      if (battlefield[newRow][newCol].getActor().getType()==' '){
      battlefield[newRow][newCol].setActor(battlefield[row][col].getActor());
      }
      else {
      if (battlefield[row][col].getActor().getStrength()>battlefield[newRow][newCol].getActor().getStrength()){
      battlefield[newRow][newCol].setActor(battlefield[row][col].getActor());
      battlefield[row][col].getActor().setStrength(0);
      battlefield[row][col].getActor().setType(' ');
      
      }
      }
      }
      }
      This is my Main class
      *public *class *LordOfTheRings {*
      
      
      
      *public* *static* *void* main (String[] argv) { 
      
      
      
      
      
      
      
      
      Scanner in = *new* Scanner (System.+in+); 
      
      
      
      
      
      Random generator = *new* Random(); 
      
      
      Random gemerator1 = *new* Random(); 
      
      
      
      
      
      
      
      
      *int* row; 
      
      
      *int* col; 
      
      
      
      
      
      *do* { 
      
      
      System.+out+.print( "\nNumber of rows 20 max: "); 
      
      
      row = in.nextInt(); 
      
      
      } *while* (row < 0 || row > 20); 
      
      
      
      
      
      *do* { 
      
      
      System.+out+.print( "\nNumber of cols 10 max: "); 
      
      
      col = in.nextInt(); 
      
      
      } *while* (col < 0 || col > 10); 
      
      
      
      
      
      Battlefield game = *new* Battlefield(row,col); 
      
      
      
      
      
      *for* (*int* i =0; i<row; i++){ 
      
      
      *for* (*int* j =0; j<col; j++){ 
      
      
      *int* Square = generator.nextInt(10); 
      
      
      *switch*(Square){ 
      
      
      *case* 0: 
      
      
      game.setSquare(i, j, *new* Square ('m', *new* Actor(' ',0))); 
      
      
      *break*; 
      
      
      *case* 1: 
      
      
      game.setSquare(i, j, *new* Square('n', *new* Actor ('e', generator.nextInt(8)+1)));+ 
      
      
      +*break*;+ 
      
      
      +*case *2:* +
      
      
      +*+game.setSquare(i, j, *new *Square('n', *new *Actor ('o', generator.nextInt(8)+1)));+* +
      
      
      +*+*break;+*+ 
      
      
      +*default*:+ 
      
      
      +game.setSquare(i, j, *new *Square('n', *new *Actor (' ', 0)));* +
      
      
      +*+*break;+*+ 
      
      
      +}+ 
      
      
      +}+ 
      
      
      +}+ 
      
      
      +game.display();+ 
      
      
      +System.+out+.println(); 
      
      
      
      
      
      *int* choice =0;{ 
      
      
      System.+out+.println("Enter 0 to quit and 1 to continue battle"); 
      
      
      choice = in.nextInt(); 
      
      
      *while* (choice !=0) { 
      
      
      *for* (*int* r=0; r<row; r++){ 
      
      
      *for* (*int* c=0; c<col; c++){ 
      
      
      *if* (game.getSquare(r, c).getActor().getType() != ' ') 
      
      
      game.moveActor(generator.nextInt(4),r,c); 
      
      
      } 
      
      
      } 
      
      
      game.display();
       
      
      
      
      
      
      
      
      
      } 
      
      
      } 
      
      
      } 
      
      
      }
      
      Edited by: Ray014 on Feb 3, 2010 8:26 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        • 1. Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
          843789
          Copy and paste the exact error message you get.

          Also, copy and paste your code from the editor. Formatting has been borked.
          • 2. Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
            843789
            Number of rows 20 max: 5

            Number of cols 10 max: 5
            - - - - -
            | m m |
            | o|
            | m|
            | m o |
            | |

            - - - - - - -
            Enter 0 to quit and 1 to continue battle
            1
            - - - - -
            | m m |
            | o|
            | m|
            | m o o |
            | |

            - - - - - - -Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
                 at Battlefield.moveActor(Battlefield.java:133)
                 at LordOfTheRings.main(LordOfTheRings.java:70)
            public class LordOfTheRings {
                 public static void main (String[] argv) {
                      
                      
                 Scanner in = new Scanner (System.in);
                 
                 Random generator = new Random();
                 Random gemerator1 = new Random();
                 
                 
                 int row;
                 int col;
            
                 do {
                      System.out.print( "\nNumber of rows 20 max:  ");
                      row = in.nextInt();
                 } while (row < 0 || row > 20);
            
                 do {
                      System.out.print( "\nNumber of cols 10 max:  ");
                      col = in.nextInt();
                 } while (col < 0 || col > 10);
                 
                 Battlefield game = new Battlefield(row,col);
            
                 for (int i =0; i<row; i++){
                      for (int j =0; j<col; j++){
                           int Square = generator.nextInt(10);
                           switch(Square){
                           case 0:
                                game.setSquare(i, j, new Square ('m', new Actor(' ',0)));
                                break;
                           case 1:
                                game.setSquare(i, j, new Square('n', new Actor ('e', generator.nextInt(8)+1)));
                                break;
                           case 2:
                                game.setSquare(i, j, new Square('n', new Actor ('o', generator.nextInt(8)+1)));
                                break;
                                default:
                                     game.setSquare(i, j, new Square('n', new Actor (' ', 0)));
                                break;
                           }
                      }
                 }
                 game.display();
                 System.out.println();
                 
                      int choice =0;{
                      System.out.println("Enter 0 to quit and 1 to continue battle");
                      choice = in.nextInt();
                 while (choice !=0) {
                      for (int r=0; r<row; r++){
                           for (int c=0; c<col; c++){
                                if (game.getSquare(r, c).getActor().getType() != ' ')
                                     game.moveActor(generator.nextInt(4),r,c);
                           }
                      }
                      game.display();
                      
                      }
                      }
                 }
            }
            public class Battlefield {
            
                      private     int numElves;
                      private int numOrcs;
                      private int row; 
                      private int col; 
                      private Square battlefield[][];
                      
                      public Battlefield(){
                           
                      }
                      public Battlefield (int row, int col){
                          this.row = row;
                          this.col = col;
                           battlefield = new Square [row][col];
                           for (int i = 0; i < row; i++)
                                for (int j = 0; j < col; j++)
                                     battlefield[i][j] = new Square();
                                
                      }
                      
                      
                      
                      
                      public int getRow(){return row;
                      
                      }
                      
                      public int getCol(){return col;
                      
                      }
                      
                      public Square getSquare(int r, int c){//return Square(0, 0);
                      
                           return battlefield[r][c];
                      
                      }
                                
                      public int getNumOrcs(){return numOrcs;
                      
                      }
                      
                      public int getNumElves(){return numElves;
                      
                      }
                      
                      
                      public String toString(){
                           return "Row:"+row + "Col:"+col;
                      }
                      
                      public void setRow(int row){
                           this.row = row;
                      }
                      
                      public void setCol(int col){
                           this.col = col;
                      }
                      
                      public void setSquare(int row,int col, Square square){
                           battlefield[row][col] = new Square (square);
                           
                      }
                      public void display ( ) {
                           // display top row of ---
                           for (int i = 0; i < col; i++)
                                System.out.print (" -");
                           System.out.println();
                           for (int i = 0; i < row; i++) {
                                System.out.print("|");
                           
                                for (int j = 0; j < col; j++) 
                                     System.out.print(battlefield[i][j].toString())     ;
                                System.out.println("|");
                                
                           }
                           System.out.println();
                           for (int i = 0; i < col+2; i++)
                                System.out.print (" -");
                      
                      }
                      
                      
                      public void moveActor(int direction,int row,int col){
                      /*     int newRow =row + 1;
                           int newCol =col + 1;
                           if (direction == 0){
                                newRow = row + 1;
                                newCol = col + 0;}
                           if (direction == 1){
                                newRow = row - 1;
                                newCol = col + 0;}
                           if (direction == 2){
                                newRow = row + 0;
                                newCol = col - 1;}
                           if (direction == 3){
                                newRow = row + 0;
                                newCol = col + 1;}*/
                           int newRow = 0;
                           int newCol = 0;
            
                           if (direction == 0){
                           newRow = row - 1;
                           newCol = col;}
                           if (direction == 1){
                           newRow = row + 1;
                           newCol = col;}
                           if (direction == 2){
                           newRow = row + 0;
                           newCol = col + 1;}
                           if (direction == 3){
                           newRow = row + 0;
                           newCol = col - 1;}
            
                           if (battlefield[row][col].getTerrain()=='m')
                                return;
                           
                           if (battlefield[row][col].getActor().getType()==' ')
                                return;
                           
                      //     if ((battlefield[newRow][newCol].getActor().getType()=='e')
                      //          battlefield[row][col].getActor().getType()== " ")
                      //               return;
                           
                                if (direction ==0&&row==0)return;
                                else if (direction ==1&&row==this.row-1)return;
                                else if (direction ==2&&col==0)return;
                                else if (direction ==3&&col==this.col-1)return;
                                else {;}
                                //else if (battlefield[newRow][newCol].getTerrain() == 'm')  return; 
                                
                                 if (battlefield[row][col].getActor().getType()==(battlefield[newRow][newCol].getActor().getType()))
                                return;
                                
                                
                                
                                if (battlefield[row][col].getActor().getStrength()>battlefield[newRow][newCol].getActor().getStrength())
                                     if (battlefield[newRow][newCol].getActor().getType()==' '){
                                          battlefield[newRow][newCol].setActor(battlefield[row][col].getActor());
                                     }
                                     else {
                                          if (battlefield[row][col].getActor().getStrength()>battlefield[newRow][newCol].getActor().getStrength()){
                                               battlefield[newRow][newCol].setActor(battlefield[row][col].getActor());
                                               battlefield[row][col].getActor().setStrength(0);
                                               battlefield[row][col].getActor().setType(' ');
                                     
                                          }
                                }
                           }
                      }
            • 3. Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
              843789
              Ray014 wrote:
              - - - - - - -Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
                   at Battlefield.moveActor(Battlefield.java:133)
              If you create an array with a length of 5, the valid indicies are 0 - 4. Therefore 5 is not valid but on line 133 it is attempting to access either row or column 5 of your 2d array. Time for some debugging to find out why row or column reaches 5.