NullPointer on adding animation listener

Heya

I’ve been following the testOgreAnim demo, but I can’t figure out what I’m doing wrong for setting up my animation listener. I’m getting a nullpointerexception when I’m adding a single listener.



the method we are putting this animation in also controls the physics, so you can skip the forst part of this if you like. Below is the constructor which is where I also initialize the controller and add the listener



[java] public class WorldPhysics extends AbstractAppState implements ActionListener, PhysicsCollisionListener, AnimEventListener {



private BulletAppState bulletAppState;

private PhysicsSpace physicsSpace;

private CapsuleCollisionShape capsuleShape;

private CharacterControl character_phys;

private Spatial player;

private AppStateManager stateManager;

private Node moveTo;

private DefaultCamSystem camSys;

private Geometry mark;

private Node rootNode;

private InputManager inputManager;

private Vector3f walkDirection;

private float airTime = 0;

private AnimChannel avatarChannel;

private AnimControl avatarControl;

private ArrayList<Spatial> players;

private boolean left = false, right = false, up = false, down = false;



public WorldPhysics(AppStateManager stateManager, Spatial WorldRoot, Spatial player, DefaultCamSystem camSys, InputManager inputManager) {



this.inputManager = inputManager;

setupKeys();

bulletAppState = new BulletAppState();

this.stateManager = stateManager;

this.camSys = camSys;

walkDirection = new Vector3f();

stateManager.attach(bulletAppState);

//Sets up the physics space for this bulletappstate

physicsSpace = bulletAppState.getPhysicsSpace();

physicsSpace.setGravity(new Vector3f(0f, -2f, 0f));

physicsSpace.setAccuracy(0.005f);



//This is a temporary Player physics to get a feel of working with physics

this.player = player;

capsuleShape = new CapsuleCollisionShape(1.5f, 1f, 1);

character_phys = new CharacterControl(capsuleShape, 5f);

this.player.addControl(character_phys);

physicsSpace.add(this.player);

character_phys.setApplyPhysicsLocal(true);

players = new ArrayList<Spatial>();



//setupAnimationController

avatarControl = player.getControl(AnimControl.class);

avatarControl.addListener(this);

avatarChannel = avatarControl.createChannel();



for (String names : avatarControl.getAnimationNames()) {

System.out.println(names);

}



avatarChannel.setAnim("Idle");



}[/java]



The specific Error I’m getting is:

Oct 6, 2011 3:59:36 PM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,6,main]
java.lang.NullPointerException
at com.benkyou.util.WorldPhysics.(WorldPhysics.java:106)
at Main.simpleInitApp(Main.java:99)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:230)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:129)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:205)
at java.lang.Thread.run(Thread.java:662)


If anyone has an idea why this is coming up null. thanks

edit: The avatar is loaded in a small separate class - its below for

[java]public class Player extends Node{
private String[] avatarLoader = {
"Models/avatars/warren/Warren_Test_1.j3o",
"Models/avatars/male/businessman.j3o"
};
private Spatial avatar;
private AssetManager assetManager;
private int id;
public Player(AssetManager assetManager, int avatarNumber){
this.assetManager = assetManager;
avatar = assetManager.loadModel(avatarLoader[avatarNumber]);
avatar.setLocalScale(5f);
this.attachChild(avatar);
}[/java]

The animControl is probably null. You sure the “player” spatial is the one that actually has the AnimControl attached?

ya it seems it is. what else could be happening?

See, a nullpointer is easy to trace. Some variable in that line was null and still a method was called on it. Just check which of the objects is null in line 106 and then find out why it is null.