We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there, I'm a fairly novice user trying to use the Fisica library.
I keep getting nullpointerexceptions whenever I try to use FBody methods within a class. For instance, here is a (simplified) version of a class I am using. If I println flagBody.getX() within the class constructor there are no problems, but when I call getPosX() from the main body of my sketch I receive a NullPointerException.
class Flag {
FCircle flagBody;
Flag() {
FCircle flagBody = new FCircle(20);
flagBody.setNoStroke();
flagBody.setFill(255,0,0);
flagBody.setPosition(100,100);
flagBody.setSensor(true);
flagBody.setStatic(true);
world.add(flagBody);
}
float getPosX() {
return flagBody.getX();
}
}
I'm sure this is down to my inexperience, but I just can't work out what I'm doing wrong.
Answers
That is classic! Within Flag's constructor, you're re-declaring field flagBody as a local-scoped variable @ line #6!
Oh dear, that's so glaring isn't it.
Thanks :)
Yea, it was! :-) That's why, when possible, I'd much prefer declare & assign at the same time ASAP! *-:)