Draw a PShape from a PShape array

Hello, I’m getting a “NullPointerException” trying to draw a PShape from an array of the same type.
This is the code snippet:

PShape s10, s15, s20, s25, s30, s35, s40, s45, s50;

PShape[][] grid = {
  {s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50},
  {s50, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s50},
  {s50, s45, s40, s40, s40, s40, s40, s40, s40, s40, s40, s40, s40, s40, s40, s40, s45, s50},
  {s50, s45, s40, s35, s35, s35, s35, s35, s35, s35, s35, s35, s35, s35, s35, s40, s45, s50},
  {s50, s45, s40, s35, s30, s30, s30, s30, s30, s30, s30, s30, s30, s30, s30, s40, s45, s50},
  {s50, s45, s40, s35, s30, s25, s25, s25, s25, s25, s25, s25, s25, s30, s30, s40, s45, s50},
  {s50, s45, s40, s35, s30, s25, s20, s20, s20, s20, s20, s20, s25, s30, s30, s40, s45, s50},
  {s50, s45, s40, s35, s30, s25, s20, s15, s15, s15, s15, s20, s25, s30, s35, s40, s45, s50},
  {s50, s45, s40, s35, s30, s25, s20, s15, s10, s10, s15, s20, s25, s30, s35, s40, s45, s50},
  {s50, s45, s40, s35, s30, s25, s20, s15, s10, s10, s15, s20, s25, s30, s35, s40, s45, s50},
  {s50, s45, s40, s35, s30, s25, s20, s15, s15, s15, s15, s20, s25, s30, s35, s40, s45, s50},
  {s50, s45, s40, s35, s30, s25, s20, s20, s20, s20, s20, s20, s25, s30, s35, s40, s45, s50},
  {s50, s45, s40, s35, s30, s25, s25, s25, s25, s25, s25, s25, s25, s30, s35, s40, s45, s50},
  {s50, s45, s40, s35, s30, s30, s30, s30, s30, s30, s30, s30, s30, s30, s35, s40, s45, s50},
  {s50, s45, s35, s35, s35, s35, s35, s35, s35, s35, s35, s35, s35, s35, s35, s40, s45, s50},
  {s50, s45, s40, s40, s40, s40, s40, s40, s40, s40, s40, s40, s40, s40, s40, s40, s45, s50},
  {s50, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s45, s50},
  {s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50, s50},
};

setup() {
  s10 = createSegment(torusRadius, radius, radians(10), cylinderHeight, sides);
  s15 = createSegment(torusRadius, radius, radians(15), cylinderHeight, sides);
  s20 = createSegment(torusRadius, radius, radians(20), cylinderHeight, sides);
  s25 = createSegment(torusRadius, radius, radians(25), cylinderHeight, sides);
  s30 = createSegment(torusRadius, radius, radians(30), cylinderHeight, sides);
  s35 = createSegment(torusRadius, radius, radians(35), cylinderHeight, sides);
  s40 = createSegment(torusRadius, radius, radians(40), cylinderHeight, sides);
  s45 = createSegment(torusRadius, radius, radians(45), cylinderHeight, sides);
  s50 = createSegment(torusRadius, radius, radians(50), cylinderHeight, sides);
} //setup

void draw() {
  for (int x = 0; x < tilesX; x++) {
    for (int y = 0; y < tilesX; y++) {
      pushMatrix();
      translate(tilesW * x, tilesH * y);
      rotateX(HALF_PI);
      rotateY(rotation[x][y]);

      // NullPointerException
      shape(grid[x][y]);

      popMatrix();
    } //for x
  } //for y
} //draw

For a visual reference, this is what I’m trying to recreate in Processing:

I’ve been able to create all the segments as PShapes, but I’m having trouble drawing them, since the ones on the outside have a different inclination from the ones on the inside, hence the matrix that defines which one to draw.

Hello @eliamedeot,

I the future please provide more details about the error.

Take a look at the resources (tutorials, references, examples, etc.) here:

For example:

There are more errors…

:)

1 Like

Apparently, you need to create the shapes before you can fill the grid

it works with String though

(your code isn’t runable, so I had to add a lot)

example:


PShape s10, s15, s20, s25, s30, s35, s40, s45, s50;



String[][] grid = {
  {
    "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50"
  }
  ,
  {
    "s50", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s40", "s35", "s35", "s35", "s35", "s35", "s35", "s35", "s35", "s35", "s35", "s35", "s35", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s40", "s35", "s30", "s30", "s30", "s30", "s30", "s30", "s30", "s30", "s30", "s30", "s30", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s40", "s35", "s30", "s25", "s25", "s25", "s25", "s25", "s25", "s25", "s25", "s30", "s30", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s40", "s35", "s30", "s25", "s20", "s20", "s20", "s20", "s20", "s20", "s25", "s30", "s30", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s40", "s35", "s30", "s25", "s20", "s15", "s15", "s15", "s15", "s20", "s25", "s30", "s35", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s40", "s35", "s30", "s25", "s20", "s15", "s10", "s10", "s15", "s20", "s25", "s30", "s35", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s40", "s35", "s30", "s25", "s20", "s15", "s10", "s10", "s15", "s20", "s25", "s30", "s35", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s40", "s35", "s30", "s25", "s20", "s15", "s15", "s15", "s15", "s20", "s25", "s30", "s35", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s40", "s35", "s30", "s25", "s20", "s20", "s20", "s20", "s20", "s20", "s25", "s30", "s35", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s40", "s35", "s30", "s25", "s25", "s25", "s25", "s25", "s25", "s25", "s25", "s30", "s35", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s40", "s35", "s30", "s30", "s30", "s30", "s30", "s30", "s30", "s30", "s30", "s30", "s35", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s35", "s35", "s35", "s35", "s35", "s35", "s35", "s35", "s35", "s35", "s35", "s35", "s35", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s40", "s45", "s50"
  }
  ,
  {
    "s50", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s45", "s50"
  }
  ,
  {
    "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50", "s50"
  }
  ,
};


float torusRadius=12;
float radius = 21;
float cylinderHeight=12, sides=12;

void setup() {
  size(880, 880, P3D);

  s10 = createShape(RECT, 0, 0, 50, 50);//  createSegment(torusRadius, radius, radians(10), cylinderHeight, sides);
  s15 = createShape(RECT, 0, 0, 50, 50);//createSegment(torusRadius, radius, radians(15), cylinderHeight, sides);
  s20 =createShape(RECT, 0, 0, 50, 50);// createSegment(torusRadius, radius, radians(20), cylinderHeight, sides);
  s25 =createShape(RECT, 0, 0, 50, 50);// createSegment(torusRadius, radius, radians(25), cylinderHeight, sides);
  s30 =createShape(RECT, 0, 0, 50, 50);// createSegment(torusRadius, radius, radians(30), cylinderHeight, sides);
  s35 =createShape(RECT, 0, 0, 50, 50);// createSegment(torusRadius, radius, radians(35), cylinderHeight, sides);
  s40 =createShape(RECT, 0, 0, 50, 50);// createSegment(torusRadius, radius, radians(40), cylinderHeight, sides);
  s45 = createShape(RECT, 0, 0, 50, 50);//createSegment(torusRadius, radius, radians(45), cylinderHeight, sides);
  s50 =createShape(RECT, 0, 0, 50, 50);// createSegment(torusRadius, radius, radians(50), cylinderHeight, sides);
} //setup



void draw() {
  for (int x = 0; x < grid.length; x++) {
    for (int y = 0; y < grid[0].length; y++) {
      pushMatrix();
      translate(12 * x, 12 * y);
      rotateX(HALF_PI);
      rotateY(.22);

      // NullPointerException
      println (grid[x][y]);
      switch (grid[x][y]) {
      case "s10":
        shape(s10);
        break;
      case "s15":
        shape(s15);
        break;
      case "s45":
        shape(s45);
        break;
      }

      popMatrix();
    } //for x
  } //for y
} //draw

3 Likes

Hello @eliamedeot ,

Please read:

There is a reference to this in the Guidelines:

By generating an MCVE you may be able to solve the problem before asking for help.

If you do post an MCVE we may be able to better guide you to a solution.

The Homework Policy is in the FAQ if you are not already aware:
https://discourse.processing.org/faq

:)

1 Like

Sorry @glv, I didn’t post the full code because it’s 250 lines long.
As @Chrisir noted, I started filling the grid before creating the shapes. By moving the whole part in the setup() now everything is working. Thanks @Chrisir!

2 Likes

An apology to me is not necessary.

When you focus on process you become resourceful and a better coder in the long run.

The Guidelines have some good advice in there.

Enjoy the adventure!

:)

1 Like

Just leaving an observation:

In Java, and many other languages, when we access a variable, its content is read immediately.

B/c all those s## variables are still null when you create the 2d-array, that’s the value you’re filling the array with.

It doesn’t matter if you assign PShape values to those s## variables later, it won’t magically change the null previously stored in the array.

In short, Java arrays can’t store actual variables, but the current value stored in variables.

4 Likes

In some languages there is a way to cause a string to be treated as a variable name. Lisp (at least some dialects), R, probably others. Is this not possible in Java? (My only contact with Java so far is via Processing, so this isn’t yet obvious to me.)

You can use a Hashmap to go from String to PShape

1 Like