I'm creating a game where thats turn-based. In this game - I have a grid of N x N size.
I thought that it would be a great idea to use a 2D-boolean array to represent the binary states of all the cells in this grid.
Imagine:
// T = true | F = false
---------------------
| F | | | | |
---------------------
| | T | | | |
--------------------- // i.e. grid[0][0] = false
| | | | | |
---------------------
| | | | | |
---------------------
Each square can either be true or false. The rules of the game are not important... Just note that each cell can be true or false.
This is what I tried to implement:
public class Life {
Boolean[][] grid;
public Life(int x, int y, Boolean status) {
if(!this.grid[x][y]) {
this.grid = new Boolean[x][y];
}
this.grid[x][y] = status;
}
}
Which I would instantiate like this:
new Life(0,0,false);
new Life(2,1,true);
However, when I do this, my program crashes and I'm not sure as to what I'm doing wrong. Any help is appreciated.
Caused by: java.lang.NullPointerException at Life.(Life.java:6)