Null Pointer Exception when setPauseOnLostFocus(false) is set

I will first of all say my java isn’t perfect. Its also 2am. If im being an idiot throw me a tutorial link and as many sarchastic insults as you like!

We have a JMonkey canvas within a swing GUI. Since the game has to be restarted frequently with alot of parameters changed we decided to be lazy and do the folowing

[java]public void updateCanvas() {
canvas.removeAll();
if (evacSim != null) {
evacSim.destroy(); /NOT GOOD! Fix if possible!/
}
evacSim = new EvacSim(settings);
evacSim.passParent(this); //Get rid of this

    evacSim.createCanvas(); // create canvas!
    ctx = (JmeCanvasContext) evacSim.getContext();
    ctx.setSystemListener(evacSim);
    ctx.getCanvas().setSize(canvas.getSize());

    canvas.add(ctx.getCanvas(), g);
}[/java] 

Now for weeks, that has worked fine. I realise the destroy is dirty, and passing the GUI to the game isn’t ideal but those we are planning to fix eventually.
However, since the canvas is within a Swing container, obviously it loses focus when controls are clicked. This can to some extent be overcome by ending the controls methods by passing focus back to the canvas but its not ideal (drop down boxes for example are a real pain).
Hence we thought it sensible to turn off setPauseOnLostFocus.

With this done the code runs fine, until you call the method above for the second time (evacSim is originally null). The following error is thrown.

[java]
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,6,main]
java.lang.NullPointerException
at com.jme3.app.Application.update(Application.java:606)
at com.jme3.app.SimpleApplication.update(SimpleApplication.java:230)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:151)
at com.jme3.system.lwjgl.LwjglCanvas.runLoop(LwjglCanvas.java:229)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:228)
at java.lang.Thread.run(Thread.java:722)
[/java]

Now, this doesnt seem to cause any issues, things work as they should. So my questions are -

  • Can this just be ignored and the exception caught somewhere? If so where? I would guess in my class EvacSim extending SimpleApplication but as said, my java isnt perfect and the exception doesnt give much in the way of clues.

-If this is not ignorable ie could be causing a problem advice on how to fix it / where to look would be appreciated?

Thanks
Hector

It somewhat feels like a special case, regarding how you are using the application, being restarted every so often.
Considering its an open source project, the first thing you should do is look into the stacktrace by looking at the sources of the classes throwing the error.

Pretty sure noone will know exacly what is going wrong from the code you posted.

1 Like

You are doing everything in one method and call destroy() directly, I guess its normal to expect things to go awkward. Theres really no need to create a new application each time you open the window and if you need multiple windows you will have to move to AwtPanels and multiple views anyway (see TestAwtPanels). Also theres no need to use the jme input system if you have to work around issues of redirection, just do everything from your normal swing callbacks, and enqueue the calls to the application.
The content of this post is meant to be read as a straight information or question without an implicit dismissive stance or interest in having the other party feel offended unless there’s emotes that hint otherwise or there’s an increased use of exclamation marks and all-capital words.

1 Like

Thanks for the quick replies.

You’re right normen, it was a pretty stupid way of doing things. Just compared to the time run setting up the simulator initually scrapping the game didnt really add any overhead.
I added a method to clean up the rootNode and re-run the simpleInitApp method.

The other issue i have is consistently high CPU usage when the App is running. Is this normal? Around 70% plus readings. Its a Core i5 hyperthreaded dual core running a debian based linux.
Only integrated graphics.

If not what are the likely causes? Id initially thought some of our worker threads wern’t terminating as they should but the problem occurs even before they are started.

Re-running the simpleInit is also not a good idea. You don’t seem to be aware that there is a thread that manages this, so this is why you get exceptions: You interfere with its data. I suggest looking at the code of the engine more if you use in such unconventional ways or just reading the documentation. I honestly don’t know how you come up with such code if you just look at one bit of javadoc or documentation…? I suggest at least adding the javadoc to the sources in whatever IDE you use or keeping a bookmark for reference.
The content of this post is meant to be read as a straight information or question without an implicit dismissive stance or interest in having the other party feel offended unless there’s emotes that hint otherwise or there’s an increased use of exclamation marks and all-capital words.

I didn’t just run it as is! Since that can interfere with the update loop.
But since I needed the app more or less in the state it was in when it was first run (the only differences would also be made by running simpleInitApp) it made sense to run the method rather than copy and paste all but two lines of it!

So I made a method which enqueues a callable object to my extension of simpleApplication. This object calls rootNode.detachAllChildren() and simpleInitApp()
hence more or less restoring the app to its initial state taking into any account any changes to my custom Settings class.