I was working on recreation of the 1970 "Game of Life". But I keep on running into a NullPointerException Error. The simulation works on a 2d board, so I made a 2d array of objects of the object, boardSpace. I have no idea why the NullPointerException happens, all other forums have told me that I need to initialize everything properly. I feel like I have. Here's the code:
import java.util.Scanner;
class simulation{
public static void main(String[] args){
boardSpace[][] board = new boardSpace[30][25];
for (int a = 0; a < 5; a++){ //All these set initial values
for (int b = 0; b < 25; b++){
board[a][b] = new boardSpace(a, b);
board[a][b].state = false;
}
}
for (int c = 5; c < 10; c++){
for (int b = 0; b < 25; b++){
if (b % 2 == 1){
board[c][b] = new boardSpace(c, b);
board[c][b].state = false;
}else{
board[c][b] = new boardSpace(c, b);
board[c][b].state = true;
}
}
}
for (int d = 10; d < 15; d++){
for (int b = 0; b < 25; b++){
if (b % 2 == 1){
board[d][b] = new boardSpace(d, b);
board[d][b].state = true;
}else{
board[d][b] = new boardSpace(d, b);
board[d][b].state = false;
}
}
}
for (int e = 15; e < 20; e++){
for (int b = 0; b < 25; b++){
if (b % 2 == 1){
board[e][b] = new boardSpace(e, b);
board[e][b].state = false;
}else{
board[e][b] = new boardSpace(e, b);
board[e][b].state = true;
}
}
}
for (int f = 20; f < 25; f++){
for (int b = 0; b < 25; b++){
board[f][b] = new boardSpace(f, b);
board[f][b].state = false;
}
}
System.out.println("Let's begin");
Scanner input = new Scanner(System.in);
double command = 100.0;
while (command != 1){
if (command == 100.0){
for (int x = 0; x < 30; x++){
for (int y = 0; y < 25; y++){
printer(board); //line 69
board[x][y].checker(board);
}
}
printer(board);
for (int x = 0; x < 30; x++){
for (int y = 0; y < 25; y++){
board[x][y].numNeighbors = 0;
}
}
}
System.out.println("0 to stop simulation or 100.0 for next gen");
command = input.nextDouble();
}
}
public static void printer(boardSpace[][] grid){
for (int v = 0; v < 30; v++){
for (int w = 0; w < 25; w++){
if (grid[v][w].state == true){ //line 88
if (w == 24){
System.out.println("X");
}else{
System.out.print("X");
}
}else{
if (w == 24){
System.out.println("-");
}else{
System.out.print("-");
}
}
}
}
}
}
class boardSpace{
int xValue; //recognizes where the thing is
int yValue;
boolean state; //sees whether the space has a thing in it or not
int numNeighbors;
public boardSpace(int xPos, int yPos){
xValue = xPos;
yValue = yPos;
}
public void checker(boardSpace[][] grid){ //checks for neighbours
if (grid[xValue - 1][yValue - 1].state == true){
numNeighbors++;
}
if (grid[xValue - 1][yValue].state == true){
numNeighbors++;
}
if (grid[xValue - 1][yValue + 1].state == true){
numNeighbors++;
}
if (grid[xValue][yValue - 1].state == true){
numNeighbors++;
}
if (grid[xValue][yValue + 1].state == true){
numNeighbors++;
}
if (grid[xValue + 1][yValue - 1].state == true){
numNeighbors++;
}
if (grid[xValue + 1][yValue].state == true){
numNeighbors++;
}
if (grid[xValue + 1][yValue + 1].state == true){
numNeighbors++;
}
}
public void stateChanger(){
if (numNeighbors <= 1){
state = false;
}else if(numNeighbors == 2){
state = state;
}else if(numNeighbors == 3){
state = true;
}else if(numNeighbors >= 4){
state = false;
}
}
}
Then the output shows the beginning position of the board, which is correct, but it is followed by a message saying that at lines 88 and 69 (marked on code as comments) there is a NullPointerException error:
Let's begin
-------------------------
-------------------------
-------------------------
-------------------------
-------------------------
X-X-X-X-X-X-X-X-X-X-X-X-X
X-X-X-X-X-X-X-X-X-X-X-X-X
X-X-X-X-X-X-X-X-X-X-X-X-X
X-X-X-X-X-X-X-X-X-X-X-X-X
X-X-X-X-X-X-X-X-X-X-X-X-X
-X-X-X-X-X-X-X-X-X-X-X-X-
-X-X-X-X-X-X-X-X-X-X-X-X-
-X-X-X-X-X-X-X-X-X-X-X-X-
-X-X-X-X-X-X-X-X-X-X-X-X-
-X-X-X-X-X-X-X-X-X-X-X-X-
X-X-X-X-X-X-X-X-X-X-X-X-X
X-X-X-X-X-X-X-X-X-X-X-X-X
X-X-X-X-X-X-X-X-X-X-X-X-X
X-X-X-X-X-X-X-X-X-X-X-X-X
X-X-X-X-X-X-X-X-X-X-X-X-X
-------------------------
-------------------------
-------------------------
-------------------------
-------------------------
Exception in thread "main" java.lang.NullPointerException
at simulation.printer(sfAssign1.java:88)
at simulation.main(sfAssign1.java:69)
I'm sorry if this is a really obvious mistake I'm making. I'm extremely new to Java programming...