Hello Animation Tut Control.addListner(this);

I’m trying to have a model with animations. I copied the code from hello animation and I get this error message

[java]
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at mygame.Main.simpleInitApp(Main.java:40)
[/java]

which is this line

control.addListener(this);

The only part of the tutorial code that I changed was which model it loads. In the scene composer I find my model and multiple entries for my idle animation. Only one of them actually plays the animation. However the animation does play.

Any ideas as to what is wrong?

[java]
package mygame;

import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.animation.AnimEventListener;
import com.jme3.animation.LoopMode;
import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;

/** Sample 7 - how to load an OgreXML model and play an animation,

  • using channels, a controller, and an AnimEventListener. */
    public class Main extends SimpleApplication
    implements AnimEventListener {
    private AnimChannel channel;
    private AnimControl control;
    Node player;
    public static void main(String[] args) {
    Main app = new Main();
    app.start();
    }

@Override
public void simpleInitApp() {
viewPort.setBackgroundColor(ColorRGBA.LightGray);
initKeys();
DirectionalLight dl = new DirectionalLight();
dl.setDirection(new Vector3f(-0.1f, -1f, -1).normalizeLocal());
rootNode.addLight(dl);
player = (Node) assetManager.loadModel(“Models/Character01.j3o”);
player.setLocalScale(0.5f);
rootNode.attachChild(player);
control = player.getControl(AnimControl.class);
control.addListener(this);
channel = control.createChannel();
channel.setAnim(“stand”);
}

public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
if (animName.equals(“Walk”)) {
channel.setAnim(“stand”, 0.50f);
channel.setLoopMode(LoopMode.DontLoop);
channel.setSpeed(1f);
}
}

public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
// unused
}

/** Custom Keybinding: Map named actions to inputs. */
private void initKeys() {
inputManager.addMapping(“Walk”, new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(actionListener, “Walk”);
}
private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals(“Walk”) && !keyPressed) {
if (!channel.getAnimationName().equals(“Walk”)) {
channel.setAnim(“Walk”, 0.50f);
channel.setLoopMode(LoopMode.Loop);
}
}
}
};
}
[/java]

So I figured it out what was the problem. If you take your model into the scene composer and see a node within a node within other nodes it won’t find the AnimControl.
[java]
control = player.getControl(AnimControl.class);
[/java]
The above code will only look at the main node and none of the sub nodes for AnimControl. So you can solve this by either moving everything under the main node or you can make it look in a subnode with the following code.

[java]
control = player.getChild(“ModelSubnode”).getControl(AnimControl.class);
[/java]

An entire thread from problem to solution where I was the only one talking… sigh hopefully this helps people in the future, added a note to the wiki.