# Problem with passing vectors to next

**URL:** <https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077>\
**Category:** Coding Questions\
**Created:** [January 1, 2019, 8:26pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077 "2019-01-01T20:26:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Taxicletter](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@Taxicletter](https://discourse.processing.org/u/Taxicletter)\
**Post date:** [January 1, 2019, 8:26pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/1 "2019-01-01T20:26:04Z")

</div>

Hi,  
I have been working on an idea a while ago and want to pick up where I left. What I want to create is some kind of self-generating irregular pattern, existing of different polygons.  
I wrote the code for a random polygon, which worked.

Where I’m stuck is: I want to start the NEXT polygon in one of the corners of the previous one. So I need to pass those coordinates. I’m not succeeding in doing that.

The sketch is here: [https://www.openprocessing.org/sketch/310023](https://www.openprocessing.org/sketch/310023)

There is another small problem, on OpenProcessing, the sketch works, but in Processing on my Mac, I get a NullPointerExeption for polygon.x (while that should have value 300??)

(j=0, so nextpolygon is assigned the value (300, 300). I added the print-statement to narrow down the problem, at first, the exception was in the next line with nextpolygon.x)

```auto
Polygon[] polygon = new Polygon[1];
PVector endcorner, nextpolygon;

int j=0;

void draw() {

  if (j==0) { // the first time, when j=0, start at the center
    PVector nextpolygon = new PVector(300, 300);
  } else if (j>0) { // after that, start
    PVector nextpolygon = polygon[j].endCorner();
  }

  println(nextpolygon.x);

```

---

<div class="post-metadata">

**Author:** ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)\
**Post date:** [January 1, 2019, 10:57pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/2 "2019-01-01T22:57:20Z")

</div>

I found that your code does not run in to an exception when I comment line 27, even though both _endCorner_ and _nextPolygon_ are already initialized

```auto
while (nextpolygon.x<width) { // stop drawing polygons when the edge of the screen is reached
    polygon[j] = new Polygon(nextpolygon);
    polygon[j].build();
    polygon[j].display();
    polygon = (Polygon[])expand(polygon, polygon.length+1);
    j++;
    //println(endcorner.x +" "+ nextpolygon.x + " " + " " +polygon.length);**
  }

```

I’m still working out a solution to get the polygons to reach the end of the screen, however

Edit: I found that if you initialize _endcorner_ in setup, the code never finishes compiling and you just get a white screen

---

<div class="post-metadata">

**Author:** ![Taxicletter](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@Taxicletter](https://discourse.processing.org/u/Taxicletter)\
**Post date:** [January 2, 2019, 4:26am UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/3 "2019-01-02T04:26:34Z")

</div>

Hi BennyHacker,

Thank you for your help. I saw that I sometimes wrote “endCorner” and sometimes “endcorner”. I corrected that, but it didn’t solve the problem.

The error I got on OpenProcessing is “_TypeError: endcorner is null_”, but it draws a polygon (and prints “300” beneath the screen. When I comment 27 out, after a while I get the orange background and much polygons drawn on the same place.

In Processing the error is “_NullPointerException_”, and I only get an orange background and the sketch gets stuck. This is the same when I comment line 27 out.

“endcorner”= 0 because the returning doesn’t work. It never gets to line 65, because the println is not executed…

I’ll search further tomorrow!

---

<div class="post-metadata">

**Author:** ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)\
**Post date:** [January 2, 2019, 6:03am UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/4 "2019-01-02T06:03:43Z")

</div>

Gladly! One other thing… I noticed the way you were going to be drawing these shapes seems kinda mathy and complex so I did a quick google search and apparently there’s a method called a _Voronoi Diagram_

The implementation in processing was actually very easy:

```auto
float radius;
ArrayList<Seed> seeds;
boolean hasWhite = false;
boolean update=true;
void setup() {
  size(600, 600);
  seeds = new ArrayList<Seed>();
  int r = 28;
  for (int i = 0; i < r; i++) {
    Seed s = new Seed();
    seeds.add(s);
  }
  background(255);
}
void mouseReleased() {
  seeds = new ArrayList<Seed>();
  update=true;
  int r = (int)random(15, 35);
  for (int i = 0; i < r; i++) {
    Seed s = new Seed();
    seeds.add(s);
  }
  radius=0;
  background(255);
}
void draw() {
  hasWhite=false;
  if (update) {
    //iterate through screen pixels
    for (int k = 0; k < width; k++) {
      for (int l = 0; l < height; l++) {
        //check if pixel is white
        if (get(k, l)==color(255)) {
          hasWhite=true;
          //if so, iterate through circles
          for (int i = 0; i < seeds.size(); i++) {
            //check if pixel is within the circle's radius
            if (dist(k, l, seeds.get(i).pos.x, seeds.get(i).pos.y)<radius) {
              //if so, give it a color
              set(k, l, seeds.get(i).c);
            }
          }
        }
      }
    }
    //increase radius every frame
    radius+=.25;
    //stop updating once all white pixels are gone
    update=hasWhite;
  }
}
class Seed {
  PVector pos = new PVector(random(width), random(height));
  color c = color(random(45, 245), random(45, 245), random(45, 245));
}

```

Although since you’re trying to make your own unique algorithm I encourage you to keep at it 😛

---

<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:** [January 2, 2019, 10:06am UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/5 "2019-01-02T10:06:23Z")

</div>

> [@Taxicletter](#):
>
> ```java
> void draw() {
> 
> if (j == 0) { // the first time, when j=0, start at the center
> PVector nextpolygon = new PVector(300, 300);
> } else if (j > 0) { // after that, start
> PVector nextpolygon = polygon[j].endCorner();
> }
> 
> println(nextpolygon.x);
> 
> ```

You’re declaring local variable _nextpolygon_ inside the `if () / else` code block. 😓

It means that variable is scoped inside that curly braces block and ceases to exist once that block finishes! 😰

---

<div class="post-metadata">

**Author:** ![Taxicletter](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@Taxicletter](https://discourse.processing.org/u/Taxicletter)\
**Post date:** [January 2, 2019, 7:39pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/6 "2019-01-02T19:39:55Z")

</div>

That’s indeed interesting! But you’re also right: I want to create my own sketch 🙂 . It’s a bit different from that Voronoi diagram, I want the second polygon to start in a corner of the first polygon, and so on. If it’s possible that on more then one corner a new polygon starts.

So a next step will also be, avoiding that new polygons overlap with the existing ones.

(I’m not native English, is it a little understandable what I’m saying?)

---

<div class="post-metadata">

**Author:** ![Taxicletter](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@Taxicletter](https://discourse.processing.org/u/Taxicletter)\
**Post date:** [January 2, 2019, 8:28pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/7 "2019-01-02T20:28:12Z")

</div>

I declare it in the setup, no? But indeed, I don’t give it a value. The whole purpose of this:

```auto
  if (j == 0) { // the first time, when j=0, start at the center
    PVector nextpolygon = new PVector(300, 300);
  } else if (j > 0) { // after that, start
    PVector nextpolygon = polygon[j].endCorner();
  }

```

Is to give a value on the first vector. Maybe it’s made to complex to achieve this?  
EDIT:  
Okay, to solve the problem, I changed the if/else if statement in an if statement, so I could move

```auto
PVector nextpolygon = polygon[j].endCorner();

```

outside that code block. But wherever I put it, it gives trouble.

---

<div class="post-metadata">

**Author:** ![Taxicletter](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@Taxicletter](https://discourse.processing.org/u/Taxicletter)\
**Post date:** [January 3, 2019, 12:47am UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/8 "2019-01-03T00:47:05Z")

</div>

Ok, I moved nextpolygon elsewhere, and I don’t have the _NullPointException_ anymore. Now I get the message “_The value of the local variable nextpolygon is not used_”.

```auto
Polygon[] polygon = new Polygon[1];
PVector endcorner;
PVector nextpolygon = new PVector(300, 300);
int j=0;

void setup() {
  size(600, 600);
  background(204, 100, 30);
}

void draw() {
  
println(j);

  if (j>0) {
    PVector nextpolygon = polygon[j].endcorner();
  }

  while (nextpolygon.x<width) { // stop drawing polygons when the edge of the screen is reached
    polygon[j] = new Polygon(nextpolygon);
    polygon[j].build();
    polygon[j].display();
    polygon = (Polygon[])expand(polygon, polygon.length+1);
    j++;
   // println(endcorner.x +" "+ nextpolygon.x + " " + " " +polygon.length);
  }

  noLoop();
}

class Polygon {
  PVector endcorner = new PVector();
  PVector firstcorner = new PVector();
  PVector[] points;

  Polygon(PVector start) {
    firstcorner.set(start);
  }

  void build() {
    int numbercorners= int(random(3, 8)); // select number of corners between 3 and 8
    float t = 360/numbercorners; // = 120, 90, 72, 60, 51.x, 45
    points = new PVector[numbercorners]; // create an array to store the vectors of each corner
    float rotation = random(0, 2 * PI); // = PI/5 = 36°
    points[0] = new PVector(firstcorner.x, firstcorner.y);
    for (int i=1; i < numbercorners; i++) { // calculate the vectors for each corner of the polygon 
      // i * t would generate a regular polygon, therefor adding random amount 'rotation'
      float xcorner = firstcorner.x + cos((radians(i * t)+rotation)) * (random(12, 30)); // the random number is the length of a side
      float ycorner = firstcorner.y + sin((radians(i * t)+rotation)) * (random(12, 30));
      points[i] = new PVector(xcorner, ycorner);
    }
  }

  void display() { // draw the polygon
    beginShape();
    for (int i = 0; i < points.length; i++) {
      vertex(points[i].x, points[i].y);
    }
    endShape(CLOSE);

    ellipse(points[points.length-1].x, points[points.length-1].y, 6, 6); // this is temporary, to see the last corner
  }

  PVector endcorner() { // pass the last vector to the next polygon
    int i = int(points.length-1);

    println(points[i].x+" "+" "+points[i].y);

    PVector endcorner = new PVector(points[i].x, points[i].y); // this passes the x and y values of the last point

    println(endcorner.x+" "+" "+endcorner.y);

    return endcorner;
  }
}

```

I don’t understand, in line 19 and 20, _nextpolygon_ **is** used!

---

<div class="post-metadata">

**Author:** ![BennyHacker](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bennyhacker/32/2969_2.png) [@BennyHacker](https://discourse.processing.org/u/BennyHacker)\
**Post date:** [January 3, 2019, 1:09am UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/9 "2019-01-03T01:09:50Z")

</div>

On line 16, similar to what GoToLoop said, try removing the PVector constructor and just write:

nextpolygon = polygon[j].endcorner();

---

<div class="post-metadata">

**Author:** ![Taxicletter](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@Taxicletter](https://discourse.processing.org/u/Taxicletter)\
**Post date:** [January 3, 2019, 1:17pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/10 "2019-01-03T13:17:38Z")

</div>

@BennyHacker, that doesn’t work, the first time there is no endcorner.

I added some print-commands to see where it goed wrong and somehow the returning function never gets activated (or run or called).

```auto
Polygon[] polygon = new Polygon[1];
PVector endcorner;
PVector nextpolygon = new PVector(300, 300);
int j=0;

void setup() {
  size(600, 600);
  background(204, 100, 30);
}

void draw() {

  println(j);

  if (j>0) {
    PVector nextpolygon = polygon[j].endcorner();
  }

  while (nextpolygon.x<width) { // stop drawing polygons when the edge of the screen is reached
    println("Keep drawing polygons");
    polygon[j] = new Polygon(nextpolygon);
    polygon[j].build();
    polygon[j].display();
    polygon = (Polygon[])expand(polygon, polygon.length+1);
    j++;
    // println(endcorner.x +" "+ nextpolygon.x + " " + " " +polygon.length);
  }

  noLoop();
}

class Polygon {
  PVector endcorner = new PVector();
  PVector firstcorner = new PVector();
  PVector[] points;

  Polygon(PVector start) {
    firstcorner.set(start);
  }

  void build() {
    
    println("Building polygons");
    
    int numbercorners= int(random(3, 8)); // select number of corners between 3 and 8
    float t = 360/numbercorners; // = 120, 90, 72, 60, 51.x, 45
    points = new PVector[numbercorners]; // create an array to store the vectors of each corner
    float rotation = random(0, 2 * PI); // = PI/5 = 36°
    points[0] = new PVector(firstcorner.x, firstcorner.y);
    for (int i=1; i < numbercorners; i++) { // calculate the vectors for each corner of the polygon 
      // i * t would generate a regular polygon, therefor adding random amount 'rotation'
      float xcorner = firstcorner.x + cos((radians(i * t)+rotation)) * (random(12, 30)); // the random number is the length of a side
      float ycorner = firstcorner.y + sin((radians(i * t)+rotation)) * (random(12, 30));
      points[i] = new PVector(xcorner, ycorner);
    }
  }

  void display() { // draw the polygon
  
    println("drawing polygons");
    
    beginShape();
    for (int i = 0; i < points.length; i++) {
      vertex(points[i].x, points[i].y);
    }
    endShape(CLOSE);

    ellipse(points[points.length-1].x, points[points.length-1].y, 6, 6); // this is temporary, to see the last corner
  }

  PVector endcorner() { // pass the last vector to the next polygon
  
    println("Passing vector");
    
    int i = int(points.length-1);

    println(points[i].x+" "+" "+points[i].y);

    PVector endcorner = new PVector(points[i].x, points[i].y); // this passes the x and y values of the last point

    println(endcorner.x+" "+" "+endcorner.y);

    return endcorner;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Taxicletter](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@Taxicletter](https://discourse.processing.org/u/Taxicletter)\
**Post date:** [January 3, 2019, 1:36pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/11 "2019-01-03T13:36:32Z")

</div>

> [@GoToLoop](#):
>
> You’re declaring local variable _nextpolygon_ inside the `if () / else` code block. 😓
> 
> It means that variable is scoped inside that curly braces block and ceases to exist once that block finishes!

I understand what you’re saying, but I’m trying like crazy and I can’t figure out to solve the problem! I need the if-statement to check if it’s the first polygon. _Only_ when it’s not, I want to pass vector “endcorner” to “nextolygon”.  
I tried putting the if-statement inside and around the while-statement, but that’s not the solution.

---

<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:** [January 3, 2019, 1:52pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/12 "2019-01-03T13:52:07Z")

</div>

Declare that variable outside the `if () / else` code block scope.  
Then you can assign a value to it inside the `if () / else` code block scope.

[Processing.org/examples/variablescope.html](http://Processing.org/examples/variablescope.html)

---

<div class="post-metadata">

**Author:** ![Taxicletter](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@Taxicletter](https://discourse.processing.org/u/Taxicletter)\
**Post date:** [January 3, 2019, 7:46pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/13 "2019-01-03T19:46:47Z")

</div>

I thought I declared it at the top, before my setup().

---

<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:** [January 3, 2019, 7:50pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/14 "2019-01-03T19:50:28Z")

</div>

> [@Taxicletter](#):
>
> I thought I declared it at the top, before my **setup()**.

If a variable is already declared as a global field, when we redeclare it in a local scope, we overshadow it in that scope! 😬

---

<div class="post-metadata">

**Author:** ![Taxicletter](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@Taxicletter](https://discourse.processing.org/u/Taxicletter)\
**Post date:** [January 3, 2019, 11:52pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/15 "2019-01-03T23:52:11Z")

</div>

> [@Taxicletter](#):
>
> Ok, I moved nextpolygon elsewhere, and I don’t have the _NullPointException_ anymore. Now I get the message “ _The value of the local variable nextpolygon is not used_ ”.

I’m sorry, I was not at home and answered a little to soon, as I already mentioned, the NullPointException problem was already gone. I guess the declaration is OK like it is. The new trouble is that the variable is not used, while it is used 2 lines after the if-statement.

- Maybe the scope of that variable is again the culprit? I tried declaring nextpolygon inside the _void draw()_ but that didn’t help. Also putting the “while” part inside the “if” brackets didn’t work (it was worse).
- Is it because I use nextpolygon.x in stead of the PVector? Then it is better to return nextpolygon.x and nextpolygon.y and then recompose the PVector. IF I have time tomorrow, I’ll test that.

Already a big thanx for your advice @GoToLoop and @BennyHacker

---

<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:** [January 4, 2019, 10:26am UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/16 "2019-01-04T10:26:25Z")

</div>

My advice is that you should stick to debug your sketch under PDE’s Java Mode. ☕  
Debugging code under a browser, especially for a code transpiled from Java to JS, is much worse! 🐛

---

<div class="post-metadata">

**Author:** ![Taxicletter](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@Taxicletter](https://discourse.processing.org/u/Taxicletter)\
**Post date:** [January 4, 2019, 7:04pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/17 "2019-01-04T19:04:22Z")

</div>

I’m working in Processing on my Macbook, the openprocessing sketch is just to share it.  
Error mentioned: “_The value of the local variable “nextpolygon” is not used_”, line 16.

> [@GoToLoop](#):
>
> My advice is that you should stick to debug your sketch under PDE’s Java Mode.

Console:

```auto
Keep drawing polygons
Building polygons
drawing polygons
Keep drawing polygons
Building polygons
drawing polygons
Keep drawing polygons
Building polygons
drawing polygons
Keep drawing polygons
Building polygons
drawing polygons
...

```

---

<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:** [January 4, 2019, 10:34pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/18 "2019-01-04T22:34:59Z")

</div>

> [@Taxicletter](#):
>
> Error mentioned: “ _The value of the local variable nextpolygon is not used_ ”, line 16.

This is not an error but merely a warning! ⚠

However, since you had already declared variable _nextpolygon_ as a global field, it should never be a local variable unless you redeclared it as a local variable (or as a function parameter) as well! 🤨

---

<div class="post-metadata">

**Author:** ![Taxicletter](https://avatars.discourse-cdn.com/v4/letter/t/7ab992/32.png) [@Taxicletter](https://discourse.processing.org/u/Taxicletter)\
**Post date:** [January 6, 2019, 1:39pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/19 "2019-01-06T13:39:53Z")

</div>

> [@GoToLoop](#):
>
> However, since you had already declared variable _nextpolygon_ as a global field, it should never be a local variable unless you redeclared it as a local variable (or as a function parameter) as well! 🤨

I removed the “while” statement to avoid variablescope problems there (I’ll add new statements to avoid drawing of-screen after this problem is solved).  
Then I keep getting the “nextpolygon is not used” warning. I really don’t understand!! I use it clearly: " **polygon[j] = new Polygon(nextpolygon);**" and now it is in the same scope, so that’s not the trouble anymore.  
Somehow the sketch never goes beyond j=1 either. For that, I removed the while and the endloop. But it didn’t solve the problem.

```auto
Polygon[] polygon = new Polygon[1];
PVector endcorner;
PVector nextpolygon = new PVector(300, 300);
int j=0;

void setup() {
  size(600, 600);
  background(204, 100, 30);
}

void draw() {

  if (j>0) {
    PVector nextpolygon = polygon[j].endcorner(); // give nextpolygon the value of the last vector "endcorner"
  } // this function is part of the Polygon-class

  println("nextpolygon="+nextpolygon);

  polygon[j] = new Polygon(nextpolygon);
  polygon[j].build();
  polygon[j].display();
  polygon = (Polygon[])expand(polygon, polygon.length+1);
  j++;

  println("j="+j);
}

class Polygon {
  PVector endcorner = new PVector();
  PVector firstcorner = new PVector();
  PVector[] points;

  Polygon(PVector start) {
    firstcorner.set(start);
  }

  void build() {

    println("Building polygon");

    int numbercorners= int(random(3, 8)); // select number of corners between 3 and 8
    float t = 360/numbercorners; // = 120, 90, 72, 60, 51.x, 45
    points = new PVector[numbercorners]; // create an array to store the vectors of each corner
    float rotation = random(0, 2 * PI); // = PI/5 = 36°
    points[0] = new PVector(firstcorner.x, firstcorner.y);
    for (int i=1; i < numbercorners; i++) { // calculate the vectors for each corner of the polygon 
      // i * t would generate a polygon with the same rotation between each corner,
      // therefor adding random amount 'rotation'
      // firstcorner is the center of a polygon
      float xcorner = firstcorner.x + cos((radians(i * t)+rotation)) * (random(12, 30)); // x= a + r.cosθ, y= b + rsinθ
      // r is the distance from the center to the corner, here: (random(12, 30)
      // a and b are coordinates of the center of the polygon
      float ycorner = firstcorner.y + sin((radians(i * t)+rotation)) * (random(12, 30)); // 
      points[i] = new PVector(xcorner, ycorner);
    }
  }

  void display() { // draw the polygon

    println("drawing polygon");

    beginShape();
    for (int i = 0; i < points.length; i++) {
      vertex(points[i].x, points[i].y);
    }
    endShape(CLOSE);

    ellipse(points[points.length-1].x, points[points.length-1].y, 6, 6); // this is temporary, to see the last corner
  }

  PVector endcorner() { // pass the last vector to the next polygon

    println("Passing vector");

    int i = int(points.length-1);

    println(points[i].x+" "+" "+points[i].y);

    PVector endcorner = new PVector(points[i].x, points[i].y); // this passes the x and y values of the last point

    println(endcorner.x+" "+" "+endcorner.y);

    return endcorner;
  }
}

```

Second version, where I tried to initialize in the if/else statement, gives the nullpointexception error again. I don’t understand at all!

```auto
Polygon[] polygon = new Polygon[1];
PVector endcorner, nextpolygon;
int j=0;

void setup() {
  size(600, 600);
  background(204, 100, 30);
}

void draw() {

  if (j==0) {
    PVector nextpolygon = new PVector(300, 300);
  }   
  else {
    PVector nextpolygon = polygon[j].endcorner(); // give nextpolygon the value of the last vector "endcorner"
  } // this function is part of the Polygon-class

  println("nextpolygon="+nextpolygon);

  polygon[j] = new Polygon(nextpolygon);
  polygon[j].build();
  polygon[j].display();
  polygon = (Polygon[])expand(polygon, polygon.length+1);
  j++;

  println("j="+j);
}

class Polygon {
  PVector endcorner = new PVector();
  PVector firstcorner = new PVector();
  PVector[] points;

  Polygon(PVector start) {
    firstcorner.set(start);
  }

  void build() {

    println("Building polygon");

    int numbercorners= int(random(3, 8)); // select number of corners between 3 and 8
    float t = 360/numbercorners; // = 120, 90, 72, 60, 51.x, 45
    points = new PVector[numbercorners]; // create an array to store the vectors of each corner
    float rotation = random(0, 2 * PI); // = PI/5 = 36°
    points[0] = new PVector(firstcorner.x, firstcorner.y);
    for (int i=1; i < numbercorners; i++) { // calculate the vectors for each corner of the polygon 
      // i * t would generate a polygon with the same rotation between each corner,
      // therefor adding random amount 'rotation'
      // firstcorner is the center of a polygon
      float xcorner = firstcorner.x + cos((radians(i * t)+rotation)) * (random(12, 30)); // x= a + r.cosθ, y= b + rsinθ
      // r is the distance from the center to the corner, here: (random(12, 30)
      // a and b are coordinates of the center of the polygon
      float ycorner = firstcorner.y + sin((radians(i * t)+rotation)) * (random(12, 30)); // 
      points[i] = new PVector(xcorner, ycorner);
    }
  }

  void display() { // draw the polygon

    println("drawing polygon");

    beginShape();
    for (int i = 0; i < points.length; i++) {
      vertex(points[i].x, points[i].y);
    }
    endShape(CLOSE);

    ellipse(points[points.length-1].x, points[points.length-1].y, 6, 6); // this is temporary, to see the last corner
  }

  PVector endcorner() { // pass the last vector to the next polygon

    println("Passing vector");

    int i = int(points.length-1);

    println(points[i].x+" "+" "+points[i].y);

    PVector endcorner = new PVector(points[i].x, points[i].y); // this passes the x and y values of the last point

    println(endcorner.x+" "+" "+endcorner.y);

    return endcorner;
  }
}

```

---

<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:** [January 6, 2019, 2:19pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/20 "2019-01-06T14:19:21Z")

</div>

> [@Taxicletter](#):
>
> `PVector nextpolygon = polygon[j].endcorner();`

When a datatype (PVector in this case) is placed before a variable it is called a variable declaration. 🧐

> **[Variables (The Java™ Tutorials \>        
            Learning the Java...](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html)**
>
> This beginner Java tutorial describes fundamentals of programming in the Java programming language

[Next page](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077.md?page=2)
