# Can't create a PShape in class constructor

**URL:** <https://discourse.processing.org/t/cant-create-a-pshape-in-class-constructor/10463>\
**Category:** Beginners\
**Created:** [April 19, 2019, 5:06pm UTC](https://discourse.processing.org/t/cant-create-a-pshape-in-class-constructor/10463 "2019-04-19T17:06:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyanblue](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@cyanblue](https://discourse.processing.org/u/cyanblue)\
**Post date:** [April 19, 2019, 5:06pm UTC](https://discourse.processing.org/t/cant-create-a-pshape-in-class-constructor/10463/1 "2019-04-19T17:06:05Z")

</div>

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:

```auto
----------------------------------

Polygon poly = new Polygon();

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

void draw(){
  background(255);
  poly.display();
}

//class stuff here

class Polygon{
  
  PShape s;
  
  Polygon(){
 
  s = createShape();
  s.beginShape();
  s.fill(175);
  s.vertex(77,57);
  s.vertex(99, -68);
  s.vertex(23,-51);
  s.endShape(CLOSE);
  
  }
  
  void display(){
    shape(s, mouseX, mouseY);
  }

}

```

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 19, 2019, 5:33pm UTC](https://discourse.processing.org/t/cant-create-a-pshape-in-class-constructor/10463/2 "2019-04-19T17:33:02Z")

</div>

```auto
Polygon poly;

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

```

You’re running **createShape()** before **setup()**! 😬

---

<div class="post-metadata">

**Author:** ![cyanblue](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@cyanblue](https://discourse.processing.org/u/cyanblue)\
**Post date:** [April 19, 2019, 6:18pm UTC](https://discourse.processing.org/t/cant-create-a-pshape-in-class-constructor/10463/3 "2019-04-19T18:18:36Z")

</div>

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!
