1

This is my code to load a node with a model then attach an animation control. I get a NullPointerException because it is saying my AnimControl is null.

My anim control class is in Models/stickman.blend/Armature/Cube

Node model = (Node)assetManager.loadModel("Models/stickman.j3o");
control = model.getControl(AnimControl.class);
control.addListener(this);
channel = control.createChannel();
channel.setAnim("jump");
model.scale(0.25f);
model.addControl(physicsCharacter);
getPhysicsSpace().add(physicsCharacter);
rootNode.attachChild(model);

How do I get rid of this nullpointerexception or move the AnimControl class up out of the sub directory so it won't be null? Thanks.

java.lang.NullPointerException
at mygame.Main.simpleInitApp(Main.java:96)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:226)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)
at java.lang.Thread.run(Thread.java:744)

enter image description here

Waggoner_Keith
  • 590
  • 2
  • 9
  • 37
  • 1
    Post the full stacktrace of the NPE. – m0skit0 Jan 25 '15 at 21:25
  • Does that edit help? – Waggoner_Keith Jan 25 '15 at 21:43
  • 1
    Which line is Main.java:96? – m0skit0 Jan 25 '15 at 22:27
  • control.addListener(this); – Waggoner_Keith Jan 25 '15 at 22:28
  • Check out the edit. I added a picture of where my AnimControl class is located within the model – Waggoner_Keith Jan 25 '15 at 22:31
  • Pretty obvious that `control` is null. What is the definition for `AnimControl`? – m0skit0 Jan 25 '15 at 23:08
  • From looking at the tutorials, there is one thing in particular you are not doing. [The tutorials](http://wiki.jmonkeyengine.org/doku.php?id=jme3:beginner:hello_animation) for animated models all have something like `rootNode.attachChild(player);` right after the model is loaded but right before they try and get a reference to the models `AnimControl`. I wonder if adding `rootNode.attachChild(model);` right before you try and assign `control` would fix the problem. If Not i would say something is wrong with the model's animation and so it is not loaded. – Pow-Ian Jan 26 '15 at 19:38

1 Answers1

1

The NullPointerException is telling you as clearly as it can that the variable control is null. This in turn means that model.getControl(AnimControl.class); has returned null. The javadoc tells you it will do this if there is no control of type AnimControl.class

I think, looking at the tutorial, that you must attach your Node to the rootNode. From this page

Every JME3 application has a rootNode: Your game automatically inherits the rootNode object from SimpleApplication.

So, as long as your class extends SimpleApplication you just need to add

rootNode.attachChild(model);

immediately after

Node model = (Node)assetManager.loadModel("Models/stickman.j3o");

It seems that this attachment will automatically associate your model with a control - as demonstrated in this snippet from the animation beginners tutorial

player = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
player.setLocalScale(0.5f);
rootNode.attachChild(player);
control = player.getControl(AnimControl.class);
J Richard Snape
  • 20,116
  • 5
  • 51
  • 79