Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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)
share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
So when I state the type in front, this is telling the compiler that I want to create a new variable? –  Alium Britt Apr 20 at 17:21
1  
@AliumBritt Yes. 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. –  Anubian Noob Apr 20 at 17:23
    
Ok, I was wondering why that was. Thanks for the explanation! –  Alium Britt Apr 20 at 17:49
1  
Just tested it after these changes - works like a champ now! –  Alium Britt Apr 20 at 17:58
    
@AliumBritt Glad to help :) –  Anubian Noob Apr 20 at 17:59
add comment
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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.