We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey there,
I am new to oop and don´t now why my program does not start ... The values that are given into the constructor in the "else" part are public variables frome the object created in the "if". after that always the next branch shell take the variables from the branch created bevor ...
My processing gives me: "NullPointerExeption"
when i run the code the window stays blank. after that it says: "Could not run the sketch"
// ----- The Branches -------
public void growBranch() {
// declare the ArrayObject
Branch[] branch;
// Inicialize the ArrayObject
branch = new Branch[branchNumber];
int branchIndex = 0;
for (int i = 0; i < branchNumber; i++) {
// in the first round the starting length is divided, values are taken from the plant object
if (i == 1) {
branch[branchIndex++] = new Branch(targetX, targetY, (startLength/2));
branch[branchIndex].calcBranch();
// in all other rounds, values are taken from the last branch object created.
} else {
branch[branchIndex++] = new Branch(branch[i].branchTargetX, branch[i].branchTargetY, (branch[i].growLength)/2);
branch[branchIndex].calcBranch();
}
}
}
}
Thanks i advance. Let me now if i shall provide more of the code
and sry for my bad Englich ; )
Answers
What line throws the NullPointerException? What is the value of every variable on that line? Hint: one of them is null. Use print statements to figure that out.
When you figure that out, you can work backwards to determine why that variable is null, and then you'll be able to fix your error.
Thx for the fast reply,
I went thought the code and just changed some formatting as far as I thing, and now it shows me the same error in my branch object at line 7. but there is just a float variable initialisation.
I hope its ok if i post the whole thing. I gues im totaly lost here.
main sketch
Branch Class
Plant Class
Goal is to hopfully later create Plants with that.
The problem seems to stem (no pun intended) from your growBranch() method. I don't have time to pick over it in detail; but it feels wrong to be declaring the branch array within the growBranch method. If the Plant grows branches then these are a property of the Plant and should surely be declared at its root (again no pun intended :) )...
You should make Branch, and any other future parts, a fixed property of Plant.
Take a look at this thread about a class CircleMatrix which itself instantiates and stores Circle objects:
hm, i think i don´t really get that. declaring the branch array at the root means just put it into the Class instead of the method ? When i just copy it over there it says" unexpected token: branch " ???
My comment wasn't suggesting a specific code change but a change to the general structure. After your branches have grown you might want to make them move. You will probably want to do this in relation to the plant, so it would make sense to be able to reference them from there...
I may be able to post some example code later ;)
@GoToLoop
Thx for the example it really helps me learning
@blindfisch
ok i get it. I doesn´t work yet but i think you brought me on the right path i gues
;) Thx again...
In terms of specifics
branch[branchIndex++]
in your if(i == 1) statement is almost certainly the root cause of your error. If i=1, then branchIndex will also be 1 when the condition is true. You then add 1 to it with the above, so it's value is 2 when you use it inbranch[branchIndex].calcBranch()
. Remember that arrays start at 0 so 2 is out of bounds...You shouldn't need the branchIndex variable at all and should be able to do what you want by simply referencing the value of i
Just also noticed that you're setting some variables in your class to private. IIRC that's not supported in Processing...
As I've recently explained in this forum thread below:
http://forum.processing.org/two/discussion/10964/how-does-one-go-about-making-member-fields-in-a-class-actually-private-in-processing
private
and other access level keywords are Java features and not Processing's.private
, it's gonna stay there even after all ".pde" files are concatenated as 1 ".java" file.So you're implying it has not effect?
In practice when I ran @Barashy's code, the Processing IDE was flagging the private branchTargetX/Y vars with a null-pointer exception. Once I removed the private flag from the variables the IDE correctly identified the line in which the error was occurring. That would seem to imply that slapping 'private' on your variables is going to make debugging more complex - ergo "not supported" :P
@Barashy: it's a while since I had a go at something like this. I just found time to set up a proof of concept and was reminded of the main difficulty - splitting each branch into sub-branches - which is probably what led you to your rather convoluted use of branchIndex++ when assigning to your branch array.
So here's a somewhat simpler implementation with just a single class (though see my note below):
Edit: made changes to calculation of angle to correct error in trig equations. As yet untested so this may 'break' the rendered output slightly...
sketch
Branch
There's a lot of room for improvement and added features - e.g. changing branch thickness, setting more rational angles, splitting into more branches. I'd also feel more comfortable with a separate Trunk class, with Branch probably inheriting from this, to separate the behaviour of the two objects.
The key point is that branches are stored in an array at the root of the sketch (but this could be at the root of your Plant class). That means you can iterate over your branches and update them each frame; and that will allow you to add animation...
@Barashy ... As I said above: it's a while since I tried anything like this so I thought I'd have a go at expanding on the above in p5js; since that's what I'm trying to concentrate my time on these days.
The following code uses a single for loop to generate all the branches of the tree. I tried nested loops; but that wasn't as flexible. I suspect a recursive function might also be an option. Anyway - the loop works by using some variables to count progress as each branch is populated with sub-branches. I had to draw the tree out on paper and do some proper thinking (for a change) to figure the best way to set the loop limit to ensure all branches are evenly populated. I also created a separate trunk class.
Since this is for P5 it's JavaScript based; but the concepts should be equally applicable in Processing/Java. I've tried to remove extraneous code; but there may be a few redundant bits and pieces...
Note to any P5 users wanting to test this: classes should be loaded before sketch.js! The blindfish object is a way of keeping the global namespace clean and is IMHO recommended when working in JavaScript...
sketch
classes
Hi,
first of all sry for the delay but i was very busy these days.
Thank you again! i wan´t expecting that so quick and so helpful replies. I´m realy trying to come forword with programing these days and your feed back as well realy got me on the track. I think beside you showed me a great example of how to do it there is also lots of potential to learn towards a more general understanding of code. I´ll try to return that help to this forum in at other places to strength this great community.
Sry again for my medium english skills ;)
@Barashy: no worries. I quite enjoyed the challenge :)
I've already updated my p5 code to fix some of the issues I had noted in the above and also made it so branches are added from within the 'Trunk class' (which I should now rename 'Tree' or even 'Plant'!). This basically involves moving relevant variables into the Trunk class and running the for loop currently in setup when a new Trunk is created (I've added it as a method of Trunk). I won't post any updated code here just yet; but might provide you a link later on ;)
@Barashy: I've made a lot of updates to my original code: fixed a number of bugs and made various improvements. My up-to-date Tree and Branch classes can be found on github.
Now I've got my head round Github pages (thanks to this post) I have a live demo. Sorry - I'll have to update the page and source code to give you a credit for starting me on this :)
There's still room for improvement; and so many features that could be added! One thing I realised is that I got carried away adding features early on; before I was certain the base code was working correctly. I've now fixed various errors that resulted from this. So you'll find I've removed any randomisation from branch length and angle since that was:
To get a balanced range use
random() * n - n/2
Note that a lot of the additional methods are there to enable live updating of tree and branch properties :)