# pushMatrix() NullPointerException

**URL:** <https://discourse.processing.org/t/pushmatrix-nullpointerexception/30929>\
**Category:** Coding Questions\
**Created:** [June 26, 2021, 7:49pm UTC](https://discourse.processing.org/t/pushmatrix-nullpointerexception/30929 "2021-06-26T19:49:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [June 26, 2021, 7:49pm UTC](https://discourse.processing.org/t/pushmatrix-nullpointerexception/30929/1 "2021-06-26T19:49:43Z")

</div>

Hello everyone, I am writing a game using processing in Eclipse, but I get a strange NullPointerException error when using pushMatrix in two of my classes. Even taking out the pushMatrix() and popMatrix() doesn’t work, causing the program to throw the error at translate(). I cannot figure out what the error is. What is causing the error to occur?  
Here is the code:

```auto
import processing.core.*;
public class Face extends PApplet {
  float x1;
  float x2;
  float y1; 
  float y2;
  float z1; 
  float z2;
  float size;
  boolean isFront, isBack, isRight, isLeft, isTop, isBottom;
  int col;
  PVector position;
  boolean wireframe;
  boolean switchTexture;
  public Face (float s) {
    x1 = -s/2; 
    x2 = s/2;
    y1 = -s/2; 
    y2 = s/2;
    z1 = -s/2; 
    z2 = s/2;
    size = s;
    isFront = false;
    isRight = false;
    isBack = false;
    isLeft = false;
    isTop = false;
    isBottom = false;
    wireframe = false;
    switchTexture = false;
  }
  public void display() {
    pushMatrix();
    translate(position.x, position.y, position.z);
    beginShape();
    if (!wireframe) {
      noStroke();
    } else {
      stroke(0);
      noFill();
      noTexture();
    }
    texture(Game.grass3);
    if (isFront) {
      vertex(x1, y1, z1, 0, 0);
      vertex(x2, y1, z1, size/2, 0);
      vertex(x2, y2, z1, size/2, size/2);
      vertex(x1, y2, z1, 0, size/2);
    }
    if (isRight) {
      vertex(x2, y1, z1, 0, 0);
      vertex(x2, y1, z2, size/2, 0);
      vertex(x2, y2, z2, size/2, size/2);
      vertex(x2, y2, z1, 0, size/2);
    }
    if (isBack) {
      vertex(x2, y1, z2, 0, 0);
      vertex(x1, y1, z2, size/2, 0);
      vertex(x1, y2, z2, size/2, size/2);
      vertex(x2, y2, z2, 0, size/2);
    }
    if (isLeft) {
      vertex(x1, y1, z2, 0, 0);
      vertex(x1, y1, z1, size/2, 0);
      vertex(x1, y2, z1, size/2, size/2);
      vertex(x1, y2, z2, 0, size/2);
    }
    if (isTop) {
      texture(Game.grass2);
      vertex(x1, y1, z2, 0, 0);
      vertex(x2, y1, z2, size/2, 0);
      vertex(x2, y1, z1, size/2, size/2);
      vertex(x1, y1, z1, 0, size/2);
    }
    if (isBottom) {
      vertex(x1, y2, z1, 0, 0);
      vertex(x2, y2, z1, size/2, 0);
      vertex(x2, y2, z2, size/2, size/2);
      vertex(x1, y2, z2, 0, size/2);
    }
    endShape();
    popMatrix();
  }
}

```

Note: the game class, “Game” is the class in which I make the PApplet.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 26, 2021, 8:16pm UTC](https://discourse.processing.org/t/pushmatrix-nullpointerexception/30929/2 "2021-06-26T20:16:27Z")

</div>

Hello,

This may give you some insight:

> **[Using Objects](https://happycoding.io/tutorials/processing/using-objects)**
>
> Learn how to use objects to group related variables together.

`:)`

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [June 27, 2021, 9:19am UTC](https://discourse.processing.org/t/pushmatrix-nullpointerexception/30929/3 "2021-06-27T09:19:15Z")

</div>

> [@SNICKRS](#):
>
> `position`

Looks like you have forgotten to define it.

---

<div class="post-metadata">

**Author:** ![Ov3rM1nD](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ov3rm1nd/32/1756_2.png) [@Ov3rM1nD](https://discourse.processing.org/u/Ov3rM1nD)\
**Post date:** [June 27, 2021, 11:05am UTC](https://discourse.processing.org/t/pushmatrix-nullpointerexception/30929/4 "2021-06-27T11:05:26Z")

</div>

You should pass PAppplet’s object to the constructor of your class, not extend it in your class

---

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [June 27, 2021, 1:36pm UTC](https://discourse.processing.org/t/pushmatrix-nullpointerexception/30929/5 "2021-06-27T13:36:00Z")

</div>

Thank you all, so I finished defining the position variable, and I should put a PApplet in the constructor to handle all the drawing functions?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [June 27, 2021, 1:49pm UTC](https://discourse.processing.org/t/pushmatrix-nullpointerexception/30929/6 "2021-06-27T13:49:29Z")

</div>

Hello,

Please read the link I already provided.

> [@SNICKRS](#):
>
> I finished defining the position variable,

You declared it and it is now “null”.

Look at your other variables and the constructor.

You really do not need me for this one.

I don’t want to rob you of the joy of discovery and fixing your own code.

`:)`

References:

- [https://processing.org/tutorials/objects/](https://processing.org/tutorials/objects/)
- [class / Reference / Processing.org](https://processing.org/reference/class.html)
- [Object / Reference / Processing.org](https://processing.org/reference/Object.html)
- [Using Objects - Happy Coding](https://happycoding.io/tutorials/processing/using-objects) \< From my last post
- [PVector / Reference / Processing.org](https://processing.org/reference/PVector.html)
