I encounter a NullPointerException on clickCell[r][c] = false;
and on new LifeGUI(new LifeModel(x, y, s);
and can't fix it. Please explain why this problem occurs and how I may fix it.
Code:
public LifeModel(int rows, int cols, int cellSize) {
row = rows;
col = cols;
cSize = cellSize;
for (int r = 0; r < row; r++) {
for ( int c = 0; c < col; c++) {
clickCell[r][c] = false;
}
}
}
public static void main(int x, int y, int s) {
new LifeGUI(new LifeModel(x, y, s));
}
You have to create the array object
boolean [][] clickCell = new boolean[rows][cols];
Add this command before the for loop.
More info here
If clickCell is declared somewhere else, the command should be:
clickCell = new boolean[rows][cols];
Or as GriffeyDog suggests, add the new boolean[rows][cols]
at the place where you declare the array, depending on the logic of your program.
You haven't shown where the clickcell
array was declared, but likely you have declared it but not initialized it. You may have
boolean[][] clickcell;
but need:
boolean[][] clickcell = new boolean[rows][cols];
where rows
and cols
represent the size of the array you need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.