Using a 2D Array in a Class Object - NullPointerException?

The following is a short sketch with a single new Class. In the class I’m trying to develop a 2D array in the Constructor and then use it in a method in the class. The method recognizes a variable in the constructor but does not recognize any of the elements in the 2D array? It works if you comment out the “println(aWallR[0][0]);” line at the end of the class, but if that line is included, it fails with a NullPointerException. Any suggestions will be appreciated!

// a simple sketch to test using a 2D array in a class object
Level level0, level1;
color color0, color1;
int nCell;

void setup() {
  size(500,400);
  background(255);
  color0=color(256,0,0); color1=color(0,256,0);
  nCell=2;
  level0 = new Level(color0,nCell);
  level1 = new Level(color1,nCell);
}

void draw() {
  background(255);
  level0.displayLevel(width/2,height/4,width/6);
  level1.displayLevel(width/2,3*height/4,width/6);
  noLoop();
}

class Level {
//variables 
  float xPos, yPos, levelSize; 
  color levelColor;
  int nCell;
  boolean[][] aWallR;
  boolean[][] aWallD;
  
// Constructor
  Level(color temp_levelColor,int temp_nCell) { 
  levelColor=temp_levelColor;
  nCell=temp_nCell;
  boolean[][] aWallR = new boolean[nCell][nCell];
  boolean[][] aWallD = new boolean[nCell][nCell];  
    for (int i = 0; i < nCell; i++) {
    for (int j = 0; j < nCell; j++) {
      aWallR[i][j] = true;
      aWallD[i][j] = false;
      println(i,j,aWallR[i][j],aWallD[i][j]);}
     }
  }
// methods
void displayLevel (float temp_xPos,float temp_yPos,float temp_levelSize) {
  xPos = temp_xPos; yPos = temp_yPos;
  levelSize =temp_levelSize;
  rectMode(CENTER);
  fill(levelColor);
  rect (xPos,yPos,levelSize,levelSize);
  println("nCell = ", nCell);
  println(aWallR[0][0]);
}
}//end of class``
1 Like

these variables have already been declared … you only need to allocate them here.

1 Like

Thank you for taking the time to respond - but I am unsure how to interpret your input.

In my other experiences with arrays (albeit somewhat limited) - I needed to set their scope/size with this type of statement prior to defining their values. Are you suggesting those two lines are unnecessary? If they are removed you get a NullPointerException in the class constructor when you try to define their values. They seem to be defined properly in the constructor but are not recognized in the class method?

you have already declared the two variables …

so now all you need to do is allocate them in the constructor.

aWallR = new boolean[nCell][nCell];
aWallD = new boolean[nCell][nCell]; 

as is you are declaring them twice, the compiler won’t allow that.

1 Like

I do appreciate your taking the time to provide input and recommendations.

I have tried every option I can thing of to deal with this issue. The sketch fails if you try to define them in the constructor without the “new” statement. Using the “new” declaration - and if you run the sketch, they appear to be properly defined in the constructor - but not recognized as being defined in the method.

The problems seem to associated with the method recognizing the array that is defined in the constructor, but it does recognize the int “nCell”.

I have tried to “this.” these class variables and that doesn’t help. I want object in the class to have different values of the 2D array variables as defined when each constructor in invoked … if that makes sense to you.

Thanks again, I will keep trying!

  aWallR = new boolean[nCell][nCell];
  aWallD = new boolean[nCell][nCell];  
1 Like

yea … it makes sense.

Currently, you have declared the two arrays as global members, this is fine since you want to use them in your method.

Your issue is you are declaring them again in the constructor, this is not allowed. Just allocate them here.

tldr … remove the declarations in your constructor.

If I remove the declaration in the constructor, there is a NullPointerException as it attempts to define the values of the variables in the array.

??

1 Like

Sorry … I used the wrong verbiage.

You need to allocate them in the constructor using the new keyword.

class Level {
boolean[][] my2dArr; // declare

Level(int arg1, int arg2) {
my2dArr = new boolean[arg1][arg2];  // allocate

my2dArr[0][0] = true; // define
}
1 Like

slow_izzm,

That was it.

Thanks much for your efforts. I knew it was something “simple” but my inexperience with some of these constructs caused my problem. I thought I had tried every combination but obviously not.

This is a great forum and well worth the price of “admission”.

Stay Safe…

1 Like