PShape in class throws NullPointerException

I have created a very simple class that contains a PShape (never mind why). When an instance of the class is initiated, however, I get a NullPointerException when I create the shape. Can you help me understand why this happens?

Here is my code:

MyShape a = new MyShape();

void setup() {
  size(400, 400, P2D);

void draw() {

class MyShape {
  PShape sh;

  MyShape() {
    sh = createShape(); //This throws a NullPointerException
    sh.vertex(20, 20);
    sh.vertex(60, 20);
    sh.vertex(60, 60);
    sh.vertex(20, 60);

  void display() {


  • edited July 2016

    Processing's functions which start w/ create*, load* & save* should not be used before setup() or settings().

    MyShape a; // just declare variable.
    void setup() {
      size(400, 400, P2D);
      a = new MyShape(); // no NPE here!  :~D
  • Thank you so much! I would have spent ages figuring that out.

Sign In or Register to comment.