Reputation: 45
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//
}
}
Upvotes: 0
Views: 496
Reputation: 64
java.lang.RuntimeException: Uncompilable source code This will give you a run time exception I guess.
Something like "java.lang.RuntimeException: Uncompilable source code" You have just placed an extra if in else
public void printCell()
{
if (cellState == true){
System.out.print("1");
}
else /*if*/
{
System.out.print("0");
}
}
I have commented it. Try the code now and run it. or give some condition in else if (cellState==false) which makes no sense in this case as Boolean can have either true or false but still want to give it like that will work for you... Because if you are using if you have to provide certain condition to it to work..
Upvotes: 0
Reputation: 3080
public void printCell()
{
if (cellState == true)
{
System.out.print("1");
}
else if
{
System.out.print("0");
}
}
To:
public void printCell()
{
System.out.print( cellState ? "1" : "0" );
}
You have an extra if in there.
We seriously do need your error log to debug it for you, do all of us a favor and please paste out that error log.
Upvotes: 1
Reputation: 213391
//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(); // location[i][j] not instantiated
}
System.out.print("\n");
}
In the above for loop: - you have not instantiated your Cell object in your array: -
location[i][j].printCell();
Before this code you need to do: -
location[i][j] = new Cell();
Upvotes: 3