Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:19:32+00:00 2026-06-13T01:19:32+00:00

I am attempting to create Conway’s game of life in Java, and seem to

  • 0

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//
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:19:34+00:00Added an answer on June 13, 2026 at 1:19 am
    //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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While attempting to create a replacement tablespace for USER, I accidentally created a datafile
I'm attempting to create a CustomControl which will have various properties affected by an
I'm attempting to create a single Controller class to handle all foreseeable surveys that
I am attempting to create a class in vb.net from the below JSON example.
I'm attempting to create a Parcelable class in Android so that I can pass
I am attempting to create the following class name signature: public class MyClass<T> where
I am attempting to create a keyboard hook to use in Java. After some
I'm attempting to create a linked list of length n using Python. I have
I'm attempting to create a very large image in Java like so: BufferedImage bi
In attempting to create a thread-safe container class from scratch, I've run into the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.