# NullPointerException with objects

**URL:** <https://discourse.processing.org/t/nullpointerexception-with-objects/11280>\
**Category:** Coding Questions\
**Created:** [May 15, 2019, 4:14pm UTC](https://discourse.processing.org/t/nullpointerexception-with-objects/11280 "2019-05-15T16:14:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DoubleTony](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@DoubleTony](https://discourse.processing.org/u/DoubleTony)\
**Post date:** [May 15, 2019, 4:14pm UTC](https://discourse.processing.org/t/nullpointerexception-with-objects/11280/1 "2019-05-15T16:14:23Z")

</div>

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.

```auto
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() {
  }
}

```

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [May 15, 2019, 4:41pm UTC](https://discourse.processing.org/t/nullpointerexception-with-objects/11280/2 "2019-05-15T16:41:30Z")

</div>

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

```auto
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();
      }
    }
  }

```

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [May 15, 2019, 4:43pm UTC](https://discourse.processing.org/t/nullpointerexception-with-objects/11280/3 "2019-05-15T16:43:23Z")

</div>

Hi,

You made a mistake in your Maze constructor.

When you do that:

```auto
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:

```auto
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…
