0

First of all, sorry if I've formatted anything wrong here as this is my first post.

I've found a project online and have attempted to complete it. It is trying to recreate The Game of Life. In a section it is asking to intialize an instance of a variable 'private boolean[][] world', expand the constructor to include this and then to modify a method 'boolean getCell(int i, int j)' to return the corresponding value in the matrix world.

The whole project can be found here: https://sites.google.com/site/dat170oop/lab2

and the part I am referring to is Step 1 just after Your Task.

From what I've done so far I am getting a NullPointerException and not sure where to go from here.

So far I've initalized the variable and extended the constructor. I've also provided a getWorld method that just returns world. When modifying the getCell method I've ran in to a few problems. I first attempted to include a boolean b in the arguments for the method but felt like that's not the way forward. Instead I've included the variables i and j in the 2D array of world, but this gives me the NullPointerException.

    private int width, height, gen;
    private boolean[][] world; //my code

    public LifeModel (int w,int h, boolean[][] b) {
        width = w;
        height = h;
        world = b;  //my code
    }
//original code of project  for getCell method
    public boolean getCell(int i, int j) {      
        if (i==0 && j==0)
            System.out.println("LifeModel: Call to getCell(" + i + "," + j + ")");
         return(i+j) % 2 == 0;
//What I've done so far to change the method.
    public boolean getCell(int i, int j) {  

        world[2][4] = true;  //I think these should be somwhere in the main method instead
        world[4][4] = true;

        return world[i][j];
    }

I should be getting a model of the the GameOfLife with all the cells dead except for 2, but instead I am getting the NullPointerException.

I'd appreciate any explanation of where I'm going wrong or hints of what to do instead.

  • It appears that your `world` variable is never initialized. Verify that `LifeModel()` is being constructed with the correct values. This would be a good time to research how to use a debugger. – Jordan Jul 18 '19 at 18:03
  • Hi, thanks for the reply. I've changed the constructor to include ```this.world = new Boolean [50][50]; ```. When im constructing the object I have ```LifeModel model = new LifeModel(50,50, );``` but have no idea what to put for the last argument. Also, I'm looking in to using a debugger now. – fmyrdltsi Jul 18 '19 at 18:44

0 Answers0