I am attempting to create Conway’s game of life in Java, and seem to have hit a brick wall.
I created a class called “Cell,” which holds a boolean variable that essentially determines whether the cell is alive or dead, and methods to kill or create a cell when needed. In my main method, I take the number of rows and columns the user wants in their game and am trying to create an array of Cell objects each named “location.” When I attempt to run the code and print the initial value of each cell, I get a null pointer exception error and am not sure how to fix it. As far a I know, each array should not have a null value, however I am very new to this…
//Create a Cell class for the purpose of creating Cell objects.
public class Cell
{
private boolean cellState;
//Cell Constructor. Initializes every cell's state to dead.
public Cell()
{
cellState = false;
}
//This function kills a cell.
//Should be called using objectName[x][y].killCell.
public void killCell()
{
cellState = false;
}
//This function creates a cell.
//Should be called using objectName[x][y]createCell.
public void createCell()
{
cellState = true;
}
public void printCell()
{
if (cellState == true)
{
System.out.print("1");
}
else if
{
System.out.print("0");
}
}
//End Class Cell//
}
This is my Cell class. If a cell is alive, a one will be printed in its place. If dead, a 0 will be in its place.
Here is my main method. The error is occurring at the line where I create an array of Cell objects. Am I doing this all wrong?
//GAME OF LIFE//
import java.util.Scanner;
import java.lang.*;
public class GameOfLife
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("\t\tWelcome to the Game of Life!");
System.out.println("\n\nDeveloped by Daniel Pikul");
System.out.print("\n\nHow many rows would you like your game to have?");
int numRows = scan.nextInt();
System.out.print("How many columns would you like your game to have?");
int numColumns = scan.nextInt();
//Create an array of cell objects.
Cell[][] location = new Cell[numColumns][numRows];
//This for loop will print out the cell array to the screen.//
for (int i = 0; i < numRows; i++)
{
for (int j = 0; j < numColumns; j++)
{
location[i][j].printCell();
}
System.out.print("\n");
}
//Prompt the user to enter the coordinates of cells that should live.
System.out.println("Input coordinates tocreate active cells.");
int xCo, yCo;
char userChoice;
//This do loop takes coordinates from the user. Every valid coordinate
//creates a living cell in that location.
do
{
System.out.print("Enter an x coordinate and a y coordinate: ");
xCo = scan.nextInt();
yCo = scan.nextInt();
location[xCo][yCo].createCell();
System.out.print("Enter another coordinate? (Y/N) ");
String tempString = scan.next();
userChoice = tempString.charAt(0);
}while(userChoice == 'Y');
//THIS IS AS FAR AS I HAVE GOTTEN IN THE PROGRAM THUS FAR//
}
}
In the above for loop: – you have not instantiated your Cell object in your array: –
Before this code you need to do: –