This site requires JavaScript, please enable it in your browser!
Greenfoot back
shrucis1
shrucis1 wrote ...

2013/11/30

Annoying NullPointer Exception

shrucis1 shrucis1

2013/11/30

#
Alright, so recently I've been working on this game, and I keep getting this java.lang.NullPointerException that's really annoying me. I'm not a novice programmer, and I've had experience debugging before, but this one I can't understand. So here's the code: (I'm not releasing the full code because only this part relates to the problem)
Ground a = (Ground) getOneIntersectingObject(Ground.class);
int aType = a.getType();
And the error points to the second line. Now, inside my Ground class I have this method:
public int getType()
{
    return type;
}
I've checked, and the Ground's type variable is set and everything, but I keep getting this error. Anyways, if you could help, that'd be great.
erdelf erdelf

2013/11/30

#
maybe u should check if a is null, i cant see another reason
shrucis1 shrucis1

2013/11/30

#
I've checked, and the Ground's type variable is set and everything
Yeah it's always set whenever I check it.
shrucis1 shrucis1

2013/11/30

#
JUST REALIZED SOMETHING. GOTTA GO TESTING.
shrucis1 shrucis1

2013/11/30

#
I'm so stupid. *facepalm* I realized that the first piece of code was returning a null Ground object Thus, when it tried null.getType(), it obviously failed. Sorry for wasting about 10 minutes of your time. XD Sometimes all it takes to solve a bug is a second look.
danpost danpost

2013/11/30

#
shrucis1 wrote...
Yeah it's always set whenever I check it.
It is always set whenever you do not check it also. An 'int' value is never 'null'.
erdelf erdelf

2013/11/30

#
shrucis1 wrote...
I realized that the first piece of code was returning a null Ground object Thus, when it tried null.getType(), it obviously failed.
which is exactly what I told u....
shrucis1 shrucis1

2013/12/1

#
Sorry, erdelf, I thought you meant that getType() was returning a null. I had made a really obvious error and put this: Ground a = (Ground) getOneIntersectingObject(Ground.class); right after if(!isTouching(Ground.class)) { Anyways, once again, sorry about that.
You need to login to post a reply.