Help wih CustomControls

I am working on creating a Zombie type game. Right now, my “zombies” are just cubes (I haven’t made a model yet). I have created a very simple and very inefficient way to give the zombies health points using a series of global variables and if statements.

I have begun looking into CustomControls to make the process of taking away health a little more efficient. I have set up my control similar to that described in the Spacial Tutorial . What I am trying to do is get the health from a cube with a specific name (I assign the cubes specific names). I tried using: [java]int testhealth = zombieNode.getChild(“Zombie1”).getControl(ZombieControl.class).getHealth();[/java] but that ended up crashing the game and im not sure why.

Did the crash produce any runtime error messages? Have you tried stepping through the code you wrote in a debugger?

@sgold Here it is the crash report. line 264 is what I listed above.

[java]Jan 03, 2014 3:51:52 PM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at mygame.Main$1.onAction(Main.java:264)
at com.jme3.input.InputManager.invokeActions(InputManager.java:169)
at com.jme3.input.InputManager.onMouseButtonEventQueued(InputManager.java:433)
at com.jme3.input.InputManager.processQueue(InputManager.java:832)
at com.jme3.input.InputManager.update(InputManager.java:882)
at com.jme3.app.Application.update(Application.java:604)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:231)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:722[/java])

1 Like

The debugger will help you determine where the null pointer originated: zombieNode, getChild(), or getControl().

If you don’t know how to use the debugger, you might try splitting line 264 into three separate statements, each on a separate line:

Spatial child = zombieNode.getChild(“Zombie1″);
ZombieControl control = child.getControl(ZombieControl.class);
int testhealth = control.getHealth();

In the long run, learning to use the debugger would be a Good Thing.

It gives me this error whenever I add the.getChild(“Zombie1”) after zombieNode and before the .getControl(ZombieControl.class).getHealth();. Before I add the .getChild(“Zombie1”) it works fine but I cant get the health for a specific box

Your reply is a little unclear. The NullPointerException should print the line number of the statement which generates the exception.

If zombieNode.getChild() is generating the NPE, that means that zombieNode is null. Perhaps it’s not initialized yet. To test this hypothesis in the debugger, one would put breakpoints on the statement which initializes zombieNode and the statement which uses it and see which executes first.

If you don’t use a debugger, the next best thing is to put printf() or log() statement before the statements which initialize and use the variable.