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 constr
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.
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.
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.
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.