NullPointerException with objects

Hi I’m creating a random maze generator but it doesn’t seem to generate the cells that make up the maze and I get a NullPointerException when I try to access the cells to give them the position of the vertices, I ask for help.

Maze labirint = new Maze(3, 3);

void setup() {
  size(610, 610);
  frameRate(5);
}

void draw() {
  background(255);
  int k1=5;
  int k2=5;
  int k3=height-5;
  int k4=width-5;
  int[] g1={k1,k2};
  int[] g2={k3,k4};
  labirint.SetGrid(g1, g2);
  labirint.ShowMaze();
}
class Block {
  int[][] vertex = new int[4][2];
  boolean visited= false;
  boolean[] Wall = {true, true, true, true};
  int[] WallColor = {0, 0, 0, 0};
  int side=0;
  boolean Start = false;
  boolean Finsh = false;

  Block() {
    for (int i = 0; i < 4; i++) {
      int[] k = {0, 0};
      vertex[i] = k;
    }
  }
  
  void ShowCell() {
    fill(WallColor[0],WallColor[1],WallColor[2],WallColor[3]);
    for (int i = 0; i < 4; i++) {
      int k = i+1;
      if (Wall[i]) {
        if (i==3) {
          k=0;
        }
        line(vertex[i][0], vertex[i][1], vertex[k][0], vertex[k][1]);
      }
    }
  }
}
class Maze {
  int Ncols;
  int Nrows;
  int[] Start ={0, 0};
  Block[][] celle;

  Maze(int x, int y) {
    Ncols = x;
    Nrows = y;
    celle = new Block[x][y];
  }
  void SetCellStartRandom() {
    int x = int(random(Ncols));
    int y = int(random(Nrows));
    celle[x][y].Start = true;
    Start[0] =x;
    Start[0] =y;
  }
  void SetGrid(int[] origin, int[] Vrt) {
    println("origin = ", origin[0], " ", origin[1], "Vrt = ", Vrt[0], " ", Vrt[1]);
    int stepCols = int((Vrt[0]-origin[0])/Ncols);
    int stepRows = int((Vrt[1]-origin[1])/Nrows);
    println("stepCols = ", stepCols, "stepRows = ", stepRows);
    for (int i = 0; i < Ncols; i++) {
      for (int j = 0; j < Ncols; j++) {
        println("i = ", i, "j = ", j);
        celle[i][j].vertex[0][0]=origin[0]+i*stepCols; \\here appears the NullPointerException
        celle[i][j].vertex[0][1]=origin[1]+j*stepRows;

        celle[i][j].vertex[1][0]=origin[0]+(i+1)*stepCols;
        celle[i][j].vertex[1][1]=origin[1]+j*stepRows;

        celle[i][j].vertex[2][0]=origin[0]+(i+1)*stepCols;
        celle[i][j].vertex[2][1]=origin[1]+(j+1)*stepRows;

        celle[i][j].vertex[3][0]=origin[0]+i*stepCols;
        celle[i][j].vertex[3][1]=origin[1]+(j+1)*stepRows;
      }
    }
  }
  void ShowMaze() {
    for (int i = 0; i < Ncols; i++) {
      for (int j = 0; j < Ncols; j++) {
        celle[i][j].ShowCell();
      }
    }
  }
  void GenerateMaze() {
  }
}

1 Like

have you assigned celle[][]?
looks like you access it before assign it first
it will look like this

Maze(int x, int y) {
    Ncols = x;
    Nrows = y;
    celle = new Block[x][y];
    for(int i = 0; i< x; i++){
      for(int j= 0; j < y; j++){
        celle[i][j] = new Block();
      }
    }
  }
3 Likes

Hi,

You made a mistake in your Maze constructor.

When you do that:

celle = new Block[x][y];

You are simply saying that celle is a double array of Block but you are not really creating the Block themselves. It is an empty array.

To correct it you need to initialize every spot of that array this way:

Maze(int x, int y) {
    Ncols = x;
    Nrows = y;
    celle = new Block[x][y];
    
    for (int i = 0; i < x; i++) {
      for (int j = 0; j < y; j++) {
        celle[i][j] = new Block();
      }
    }
  }

Edit: Did not see @humayung’s answer…

3 Likes