1

When I am creating an array of Example objects, I call something like initializeArray(); I use a simple nested for loop to traverse through the array and then assign new objects with values to each index of the array using exampleArr[i][j] = new Example(false, false, false, 0); however, calling this gives me an java.lang.ArrayIndexOutofBoundsException:0 at the line above.

I am assuming that I am instantiating the new object incorrectly, as this also happens in another method which is supposed to display all of the Example objects in the array. However, I will post the nested loop I am using in case there is something that i've done wrong that I can't see.

public void initializeArray(){
    for(int i = 0; i < getRows(); i++){
        for(int j = 0; j < getColumns(); j++){
             tileArr[i][j] = new Tile(false, false, false, 0);
        }
    }
}

//Declaration of rows and columns

    private int rows; 
    private int columns; 
    Tile[][] tileArr = new Tile[rows][columns]; 
public void setRows(int r)
{   
 rows = r; 
} 
 public void setColumns(int c)
 { 
     //various setters and getters for the array 
  columns = c; 
 } 
public int getRows()
{ 

  System.out.print(rows); 
  return rows; 
} 
public int getColumns()
{ 
  System.out.print(columns); 
  return columns; 
} 

Thanks everyone for your help! The problem has been solved.

  • `tileArr` must has a compatible size, i.e. it must be created with something like `Tile[] tileArr = new TileArr[n][m];` – Gaël J Oct 16 '15 at 14:51
  • 3
    You are initializing individual element but have not allocated memory for the array or declared size is less than your iteration count. – SacJn Oct 16 '15 at 14:51
  • Can you please post your declaration. Because if I assume that you have not allocated memory then it should have been `NullPointerException`. Please clear the clouds by posting variable declaration – SacJn Oct 16 '15 at 15:05
  • 1
    Where have you called setters for row and column – SacJn Oct 16 '15 at 15:15
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – gerosalesc Oct 16 '15 at 15:40

2 Answers2

2

Declare your tileArr at the top but do not initialize.

Tile[][] tileArr; 

Then initialize your array before your for loop in the initializeArray() (This is assuming your rows and columns is set. You can add logic to check this as well).

tileArr = new Tile[getRows()][getColumns()];
tileArr = new Tile[rows][columns]; //Do this instead if you don't want the print statements to be called
gonzo
  • 2,103
  • 1
  • 15
  • 27
  • Sorry, I should've been more clear, that's one of my instance variables at the top of the class. –  Oct 16 '15 at 15:02
  • @Bert can you show us how you initialized it then at the top? – gonzo Oct 16 '15 at 15:04
  • 1
    and also please add the implementations of getRows() and getColumns() methods in case there might be some problem with them – Megh Vidani Oct 16 '15 at 15:06
  • `private int rows; private int columns; Tile[][] tileArr = new Tile[rows][columns]; public void setRows(int r){ rows = r; } public void setColumns(int c){ //various setters and getters for the array columns = c; } public int getRows(){ System.out.print(rows); return rows; } public int getColumns(){ System.out.print(columns); return columns; }` –  Oct 16 '15 at 15:08
  • @gonzo, sorry It's not letting me format it for some reason. –  Oct 16 '15 at 15:09
  • 2
    When you initialize `tileArr` both `rows` and `columns` are `0`. You should probably declare `tileArr` at the top like you are currently doing but don't initialize until you have `rows` and `columns` set. – gonzo Oct 16 '15 at 15:10
  • @Bert also just edit your original question and put all of that code in there. Thanks. :) – gonzo Oct 16 '15 at 15:11
  • It works, thank you! Now I just need to figure out my toString method, thanks for your help! –  Oct 16 '15 at 15:22
  • @Bert Feel free to accept the answer. Glad i was able to help. :) – gonzo Oct 16 '15 at 15:22
  • @gonzo, thanks again, I accepted the answer, after i found the button lol. –  Oct 16 '15 at 15:26
0

As @gonzo said you have got to initialize your array to allocate enough memory for all the positions you are going to be using.

Tile[][] tileArr;

public void initializeArray(){
       Tile[][] tileArr = new Tile[getRows()][getColumns()];
       for(int i = 0; i < getRows(); i++){
           for(int j = 0; j < getColumns(); j++){
                tileArr[i][j] = new Tile(false, false, false, 0);
           }
       }
       return titleArr;
}

//...wherever you want it
this.tileArray = this.initializeArray()

But there are cases when you don't know how big this array may be. For those cases you should be using List<Tile> type like LinkedList<Tile> or ArrayList<Tile> so that you don't need to allocate space for every new position to use.

gerosalesc
  • 2,983
  • 3
  • 27
  • 46
  • If it was the problem of allocating memory, shouldn't it throw NPE – SacJn Oct 16 '15 at 15:08
  • @SacJn Nope because he is trying to access a position that doesn't exist in the array before even trying to use it – gerosalesc Oct 16 '15 at 15:13
  • I tried running the code for `int x[][]=null`. It throws NPE – SacJn Oct 16 '15 at 15:14
  • @SacJn try this `String [][] tileArr = new String[1][1]; System.out.println(tileArr[1][2]);` – gerosalesc Oct 16 '15 at 15:28
  • In your comment, you have allocated memory definitely but not enough. So it won't . But what your statement says **you have got to initialize your array to allocate space for its positions** is ambiguous then. Rather you can specify it for sufficient memory. Anyway there was just misunderstanding or miss of words so lets end it here – SacJn Oct 16 '15 at 15:32