Java - NullPointerException after initializing arr

2019-02-18 04:27发布

问题:

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)

回答1:

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.



回答2:

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.



回答3:

In the constructor you initialize an array boolean[]. The array is only visible in the constructor. If you want to use the array of the class, then replace

boolean[] grid = new boolean[sides];

with

grid = new boolean[sides];

. Then you use the array of the class, not of the constructor.



回答4:

In the constructor, you created a local array with the same name as the class member grid which is being shadowed by the local grid array and that is the reason for the null pointer exception since the class member was never initialized.

Simply change:

 boolean[] grid = new boolean[sides]

to

 grid = new boolean[sides]

This will ensure that you access the class member you want.