Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - NullPointerException after initializing array and when testing array with class method

I've been trying to initialize an array and then test its values with a class method. I have initialized the array and have already tested it successfully within the constructor, so it looks like the array was populated, but as soon as I try to test this in the Main method it's throwing a NullPointer exception.

class PercolGrid {
    int sides;
    boolean[] grid;

    public PercolGrid (int inSides) {
        sides = inSides;
        //grid = new boolean[] {false,false,false,false,false,
        //false,false,false,false,false};
        boolean[] grid = new boolean[sides];
        for (int i = 0; i < sides; i++) {
            grid[i] = false;
            System.out.println("Setting " + i + " to " + grid[i]);
        }
        System.out.println("Checking outside FOR loop, first square is: " + grid[0]);
    }


    public boolean testSqr (int i) {
        System.out.println("Requested index is: " + i);
        return grid[i];
    }

    public static void main(String[] args){
        PercolGrid firstGrid = new PercolGrid(10);
        System.out.println("Grid created! Checking index ....");
        System.out.println("First square is :" + firstGrid.testSqr(0)); // NullPointerException
        System.out.println("First square is :" + firstGrid.grid[0]); // and here
    }
}

It's almost like the referenced data exists within the constructor but then doesn't exist outside of it. When I comment out the for loop and the boolean[] .... line above it, and uncomment my grid = new boolean[] .... line, it all works ok, but I want to choose the number of sides when I instantiate the object.

EDIT - This same error occurs if I comment out line 19 (firstGrid.testSqr(0)) and instead run line 20 (firstGrid.grid[0]).

This is a practice using a 1D array before I try the same thing with a 2D array. What am I missing?

My output looks like this:

Setting 0 to false
...
Setting 9 to false
Checking outside FOR loop, first square is: false
Grid created! Checking index ....
Requested index is: 0
java.lang.NullPointerException
    at PercolGrid.testSqr(PercolGrid.java:19)
    at PercolGrid.main(PercolGrid.java:25)
like image 479
Alium Britt Avatar asked Apr 20 '14 16:04

Alium Britt


2 Answers

Your problem is with this line:

boolean[] grid = new boolean[sides];

This is initializing a local variable grid, not the field in the instance.

Change it to:

grid = new boolean[sides];

This initializes the field in the instance.

By putting the type in front you are declaring a new variable. When you declar a variable in a method its scope is limited to that method. Since your local variable is named the same as your instance variable it "hides" the instance variable.

like image 59
Anubian Noob Avatar answered Sep 23 '22 05:09

Anubian Noob


boolean[] grid = new boolean[sides];

Here you create a new boolean array grid which hides the class field.

Just

grid = new boolean[sides];

and you will refer to grid class field.

like image 22
Marco Acierno Avatar answered Sep 19 '22 05:09

Marco Acierno