Please help. I'm having an indexArrayOutOfBounds in my code. what's wrong with this? the compiler says the error occurs at the if ststement.
private void tableTest(){
int nRow = sampleTable.getRowCount();
int nCol = sampleTable.getColumnCount();
int counter = 0;
int j, i;
Object[][] tableData = new Object[nRow][nCol];
for (i = 0 ; i < nRow ; i++){
for (j = 3 ; j < nCol ; j++){
tableData[i][j] = sampleTable.getValueAt(i,j);
System.out.println(tableData[i][j]);
//if(counter)
// Arrays.deepToString(tableData[i][j]);
}
System.out.println("end");
if(tableData[i][j].equals(true)){
counter++;
System.out.print(counter);
}
}
}
You need that if
block to be inside the inner for
loop, or the indexes don't match to anything in the array.
Currently, that if
statement runs when j
has already reached nCol
, which means it has exceeded the bounds of the array.
To fix this, remove the }
from the line below the comments, and add a }
right at the end. This effectively moves the if
block up into the inner loop.
Update
You might get a null pointer exception calling equals
on references in the array that have not been assigned an object. You should probably change the if
condition to say this.
if(tableData[i][j] != null && tableData[i][j].equals(true)){