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.
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
1 Like
More exactly nextpolygon is being declared 3 times. At the top of the sketch as a global field:
And 2 more times within draw() in both if
& else
blocks:
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:
void draw() {
PVector nextPolygon = j == 0? new PVector(width>>1, height>>1) : polygon[j].endcorner();
println("nextPolygon =", nextPolygon, "\tj =", j);
1 Like
@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!
1 Like
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:
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.
Yes, it is. I thought it was something jargonny that you only learn by reading thick and complex books