# 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:** 6\
**Page:** 2

<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, 4:34pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/21 "2019-01-06T16:34:21Z")

</div>

> [@GoToLoop](#):
>
> > `PVector nextpolygon = polygon[j].endcorner();`
> 
> When a datatype (PVector in this case) is placed before a variable it is called a variable declaration. 🧐

Do yo mean that I’m declaring it two times and that’s the problem? I do understand now that I can write “nextpolygon = polygon[j].endcorner();”, which I didn’t know.  
But it doesn’t solve the nullpointerexception and it doesn’t solve the sketch stopping at j=1.

In the version with if/else, if I remove the declaration at the top, because of what you said (the declaration) I get “nextpolygon cannot be resolved to a variable”.  
If it is because it is between the if/else-brackets, how the hell can I ever assign a value to a parameter based on a condition??

@GoToLoop, I’m very thankful you try to help me, but you seem to think I’m a more advanced programmer than I am. The hints you give are interesting, but I don’t know how they can solve my problem. I read the page you referred to.

How does that datatype explain why Processing says the variable is not used, why 2 lines later it IS used? Or why the loop seems to stop when j=1.

---

<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 6, 2019, 7:38pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/22 "2019-01-06T19:38:49Z")

</div>

Sorry to hear you’re still having difficulty. This isn’t a guaranteed solution, but if you are really and truly stuck I’d just start working on a new version from scratch, that way you can move through the problems one step at a time instead of having many lines of code that you can’t get to work. It usually helps me when I’m stuck. You should also try to find examples online that relate to the problem

---

<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, 9:18pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/23 "2019-01-06T21:18:34Z")

</div>

> [@Taxicletter](#):
>
> Do yo mean that I’m declaring it two times and that’s the problem?

More exactly **nextpolygon** is being declared 3 times. At the top of the sketch as a global field: 😵

> [@Taxicletter](#):
>
> ```auto
> PVector endcorner, nextpolygon;
> 
> ```

And 2 more times within **draw()** in both `if` & `else` blocks: 😬

> [@Taxicletter](#):
>
> ```auto
> if (j == 0) {
> PVector nextpolygon = new PVector(300, 300);
> }
> else {
> PVector nextpolygon = polygon[j].endcorner();
> }
> 
> ```

Here’s 1 of the approaches to solve it. 😀

Given variable _nextpolygon_ is used inside **draw()** only, there’s no actual need for it to be declared as a global field. 🌎

So in this version, we remove its top declaration & simply declare it inside **draw()** as a local variable. 💡

Also, by Java’s naming conventions, _nextpolygon_ should be named as _nextPolygon_ instead: 🤓

```auto
void draw() {
  PVector nextPolygon = j == 0? new PVector(width>>1, height>>1) : polygon[j].endcorner();
  println("nextPolygon =", nextPolygon, "\tj =", j);

```

---

<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, 10:06pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/24 "2019-01-06T22:06:42Z")

</div>

@GoToLoop I didn’t post the code, but I did already remove the global declaration (no result). I thought declaring it in the two conditions of the if/else-statement, wouldn’t count for two declarations because only one or the other is accepted. I renamed nextPolygon and also endCorner.

I tried your version, but at first I still have the nullpointerexception. I’ll do what @BennyHacker advices: I’ll rewrite the code with what I learned from you both! I’m not literally going to copy your code, because it’s more advanced then I understand 😊 Don’t know when that will be, because I have to work tomorrow 🙂

I’ll let you know (and see) if I succeed in making a working version of the sketch!

👋

---

<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, 10:14pm UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/25 "2019-01-06T22:14:48Z")

</div>

> [@Taxicletter](#):
>
> I’m not literally going to copy your code, because it’s more advanced than I understand.

```auto
void draw() {
  PVector nextPolygon = j == 0? new PVector(width>>1, height>>1) : polygon[j].endcorner();
  println("nextPolygon =", nextPolygon, "\tj =", j);

```

The code above can be written like this too: 🙃

```auto
void draw() {
  final PVector nextPolygon;
  
  if (j == 0) nextPolygon = new PVector(width/2, height/2);
  else nextPolygon = polygon[j].endcorner();

  println("nextPolygon =", nextPolygon, "\tj =", j);

```

Hope it’s easier for you to understand it now. 😇

> **[?: (conditional) / Reference](https://processing.org/reference/conditional.html)**
>
> A shortcut for writing an if and else structure. The conditional operator, ?: is sometimes called the ternary operator, an operator that takes three arguments. If the test …

---

<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 7, 2019, 12:00am UTC](https://discourse.processing.org/t/problem-with-passing-vectors-to-next/7077/26 "2019-01-07T00:00:44Z")

</div>

😄 Yes, it is. I thought it was something jargonny that you only learn by reading thick and complex books 📚

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