I'm trying to draw and initialize 2d array, when all my code is in main method there is no error but i split my code to methods and i had index error.
This is my method :
public static void initializeTable(char[][] table ) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
table[i][j] = 'S'; //this is line 90 where the error occurs i think.
}
}
}
How i use it in main :
public class Cinema {
public static int row, column,x,y;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char[][] table = new char[row][column];
enterRowColumn();
initializeTable(table); //line 15
}
}
And error message :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at cinema.Cinema.initializeTable(Cinema.java:90)
at cinema.Cinema.main(Cinema.java:15)
rowandcolumnare not sync with the exact length from table. Eithertablehas length equals to zero ortable[j]has length equals to zero.