Can't create a PShape in class constructor

I’ve created a “Star” class that allows me to create a star shape given inner and outer radii and number of points. I’ve written a load of code in the constructor that works out the vertices of the star, and saves each to an array -so far so good, all working fine.

Then (still within the constructor) I wanted to write those vertices into a PShape variable that’s part of the object…so when I call my custom display() method of the Star class, I can just use shape() (instead of looping through the vertex array to draw the outline each time).

For some reason though, I can’t create a PShape object in my class’ constructor. I’m getting java.lang.reflect.invocationTargetException (and I’ve also had a “cannot initialise VM” error).
I’ve reduced, commented out and simplified the code, and the error is definitely being caused by the PShape creation.

Here’s a simplified/slimmed down version of the class (removed the code that works out angles, and substituted some hardcoded calls to vertex in its place) - the structure and scope is exactly the same as my working code:


Polygon poly = new Polygon();

void setup(){
  size(800, 800);

void draw(){

//class stuff here

class Polygon{
  PShape s;
  s = createShape();
  s.vertex(99, -68);
  void display(){
    shape(s, mouseX, mouseY);


Any idea? Is this the wrong thing to do in a class constructor?

1 Like
Polygon poly;

void setup() {
  size(800, 800);
  poly = new Polygon();

You’re running createShape() before setup()! :grimacing:


Thank you so much - d’oh, I’m such a noob! Of course that’s why it wasn’t working - thanks for saving me from tearing the rest of my hair out!