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 7508277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:39:07+00:00 2026-05-29T22:39:07+00:00

Here’s my code: http://pastebin.com/umy0FPvB (LG) and here’s the teacher’s code: http://pastebin.com/y5wU0Zpx (LCI) It’s telling

  • 0

Here’s my code: http://pastebin.com/umy0FPvB (LG)

and here’s the teacher’s code: http://pastebin.com/y5wU0Zpx (LCI)

It’s telling me I’m wrong on line 41 of the teacher’s code when the LCI is trying to read from the matrix passed from the LG [world()].

I’ve been sitting on this for a while but I can’t seem to figure out what’s wrong.

Exception in thread "main" java.lang.NullPointerException
    at Console.printWorld(Console.java:41)
    at Console.playLife(Console.java:56)
    at Console.main(Console.java:30)

—

/**
 * The Life game
 * @author Noah Kissinger
 * @date 2012.2.13
 */

import java.util.Random;

public class Life {

    private static boolean[][] matrix;
    private static int bL, bH, lL, lH, r, c;
    private static long rSeed;

    public Life(long seed, int rows, int columns, int birthLow, int birthHigh,
            int liveLow, int liveHigh) {

        rSeed = seed;
        bL = birthLow;
        bH = birthHigh;
        lL = liveLow;
        lH = liveHigh;
        r = rows;
        c = columns;

        createMatrix();

    }

    public void update() {
        updateMatrix();
    }

    public boolean[][] world() {
        return matrix;
    }

    public static void createMatrix() {

        Random seedBool = new Random(rSeed);

        boolean[][] matrix = new boolean[r][c];

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                matrix[i][j] = false;
            }
        }

        for (int i = 1; i < matrix.length - 1; i++) {
            for (int j = 1; j < matrix[i].length - 1; j++) {
                matrix[i][j] = seedBool.nextBoolean();
            }
        }

    }

    public static void updateMatrix() {

        Random seedBool = new Random(rSeed);

        boolean[][] matrixCopy = matrix.clone();
        for (int i = 0; i < matrix.length; i++)
            matrixCopy[i] = matrix[i].clone();

        int count = 0;

        for (int i = 1; i < matrix.length - 1; i++) {
            for (int j = 1; j < matrix[i].length - 1; j++) {

                if (matrix[i][j] == false) {

                    if (matrixCopy[i - 1][j - 1] == true)
                        count++;
                    if (matrixCopy[i - 1][j] == true)
                        count++;
                    if (matrixCopy[i - 1][j + 1] == true)
                        count++;
                    if (matrixCopy[i][j - 1] == true)
                        count++;
                    if (matrixCopy[i][j + 1] == true)
                        count++;
                    if (matrixCopy[i + 1][j - 1] == true)
                        count++;
                    if (matrixCopy[i + 1][j] == true)
                        count++;
                    if (matrixCopy[i + 1][j + 1] == true)
                        count++;

                    if (count >= bL && count <= bH) {
                        matrix[i][j] = true;

                        for (int i1 = 0; i1 < matrix.length; i1++) {
                            for (int j1 = 0; j1 < matrix[i1].length; j1++) {
                                matrix[i1][j1] = false;
                            }
                        }

                        for (int i1 = 1; i1 < matrix.length - 1; i1++) {
                            for (int j1 = 1; j1 < matrix[i1].length - 1; j1++) {
                                matrix[i1][j1] = seedBool.nextBoolean();
                            }
                        }
                    } else
                        matrix[i][j] = false;
                    count = 0;

                }

                else {

                    if (matrixCopy[i - 1][j - 1] == true)
                        count++;
                    if (matrixCopy[i - 1][j] == true)
                        count++;
                    if (matrixCopy[i - 1][j + 1] == true)
                        count++;
                    if (matrixCopy[i][j - 1] == true)
                        count++;
                    if (matrixCopy[i][j + 1] == true)
                        count++;
                    if (matrixCopy[i + 1][j - 1] == true)
                        count++;
                    if (matrixCopy[i + 1][j] == true)
                        count++;
                    if (matrixCopy[i + 1][j + 1] == true)
                        count++;

                    if (count >= lL && count <= lH)
                        matrix[i][j] = true;
                    else
                        matrix[i][j] = false;
                    count = 0;

                }
            }
        }
    }
}

—

/**
 * The Console class is a console interface to the Life game.
 * @author DH
 * @date Sept. 2008
 */

import java.util.Scanner;


public class Console {

    /**
     * @param args unused
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter the size of the matrix(rows, columns) :");
        int rows = in.nextInt();
        int columns = in.nextInt();
        System.out.println("Please enter random seed: ");
        long seed = in.nextLong();
        System.out.println("Please enter birth range (low, high) :");
        int birthLow = in.nextInt();
        int birthHigh = in.nextInt();
        System.out.println("Please enter live range (low, high): ");
        int liveLow = in.nextInt();
        int liveHigh = in.nextInt();
        try {
            Life game = new Life(seed, rows, columns, birthLow, birthHigh, liveLow, liveHigh);
            playLife(game);
        } catch (IllegalArgumentException e) {
            System.out.println("Inappropriate values: " + e.getMessage());
        }
    }

    /**
     * Print a boolean matrix
     * @param world is a boolean matrix to be printed with # for true and - for false.
     */
    public static void printWorld(boolean[][] matrix) {
        for (int r=0; r<matrix.length; r++) {
            for (int c=0; c<matrix[0].length; c++) {
                System.out.print(matrix[r][c] ? " # " : " - ");
            }
            System.out.println();
        }
        System.out.println();

    }

    /**
     * Play the game of Life starting with a given state
     * @param game is the Life object that provides the current state of Life
     */
    public static void playLife(Life game) {
        printWorld(game.world());
        for (int i=0; i<10; i++) {
            game.update();
            printWorld(game.world());
        }
    }

}
  • 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-05-29T22:39:08+00:00Added an answer on May 29, 2026 at 10:39 pm

    Here’s your problem. In your createMatrix() method you define a local variable matrix when you really want to modify the field matrix.

    You may find it useful to access fields with this, e.g. this.matrix. It makes a clear distinction in the code. However, most IDEs will auto highlight fields and local variables so some people find it unnecessary, it’s a question of style and not overly important.

    I haven’t checked the rest of your program, there may be other errors.

       public static void createMatrix() {
    
        Random seedBool = new Random(rSeed);
    
        this.matrix = new boolean[r][c];
    
        for (int i = 0; i < this.matrix.length; i++) {
            for (int j = 0; j < this.matrix[i].length; j++) {
                this.matrix[i][j] = false;
            }
        }
    
        for (int i = 1; i < this.matrix.length - 1; i++) {
            for (int j = 1; j < this.matrix[i].length - 1; j++) {
                this.matrix[i][j] = seedBool.nextBoolean();
            }
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my persistence.xml : <?xml version=1.0 encoding=UTF-8?> <persistence xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd version=1.0>
here is my configuration: http://domain.com (obviously fictitious name...) hosted on a server running Apache
Here's my Manifest: <?xml version=1.0 encoding=utf-8?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package=com.mappp.mobile android:versionCode=1 android:versionName=1.0 > <supports-screens android:largeScreens=true
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here is my code, which takes two version identifiers in the form 1, 5,
Here is an example: I write html code inside of textarea, then I swap
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is my code sample, let me know if it can be further improved?

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.