Fisica NullPointerException

edited July 2014 in Library Questions

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

  • edited July 2014 Answer ✓

    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 :)

  • edited July 2014

    Yea, it was! :-) That's why, when possible, I'd much prefer declare & assign at the same time ASAP! *-:)

Sign In or Register to comment.