Null Pointer Exception

Hi!



I’m very new in Java Monkey Engine and I have the following code now:

[java]public class Ball

{

Node scene;

Node player;



Ball(Node s)

{

Sphere b = new Sphere(“My Ball”, 30, 30, 15);

b.setModelBound(new BoundingSphere());

b.updateModelBound();



scene = s;

player = new Node(“Player Node”);

player.setLocalTranslation(new Vector3f(100,0, 100));

scene.attachChild(player);

player.attachChild(b);

player.updateWorldBound();

}



public Vector3f getPlayerLocalTranslation()

{

return player.getLocalTranslation();

}



public BoundingVolume getPlayerWorldBound()

{

return player.getWorldBound();

}

}

[/java]

But it doesn’t work. I get a Null Pointer Exception when I call getPlayerLocalTranslation() or getPlayerWorldBound().

Why? What’s wrong?



(I hope the question isn’t too stupid even for a beginner.)

Perhaps you’re doing something like this?

Code:
Ball b = new Ball(node); b.player = null; b.getPlayerLocalTranslation();
Since the "Node player" variable is package private, it could be set from outside code. Consider making "player" a private variable.
Momoko_Fan said:
Perhaps you're doing something like this?
Code:
Ball b = new Ball(node); b.player = null; b.getPlayerLocalTranslation();
Since the "Node player" variable is package private, it could be set from outside code. Consider making "player" a private variable.


I kind of doubt he'd be setting a field to null and then wondering why he's getting NPE's..
sbook said:
I kind of doubt he'd be setting a field to null and then wondering why he's getting NPE's..

Ooohh.. the things we have seen... ;)

Given the class he has posted, it is absolutely impossible to have player set to null any other way. It is set in the constructor, which must be called to create the object.

Maybe I am missing something?

Can you post the npe with the stacktrace? Maybee that helps

For the amount of information that has been provided, OP may just be forgetting to instantiate the Ball object :stuck_out_tongue:



@tdc2 Please let us help you!