Nifty gui issue (solved)

Hi. I’m having some troubles with Nifty. Here’s the code:


public class Main extends SimpleApplication {

public void backFromMenu() {

inMenu = false; // leave gui

// close nifty





private void setupMenu() {

inMenu = true; // enter gui

guiNode.detachAllChildren(); // remove present gui objects

niftyDisplay = new NiftyJmeDisplay(

assetManager, inputManager, audioRenderer, guiViewPort);

nifty = niftyDisplay.getNifty();

nifty.fromXml(“assets/Interface/test.xml”, “start”);

// attach the nifty display to the gui view port as a processor


flyCam.setDragToRotate(true); // disable the fly cam




setupMenu is called when I want to enter my gui - the method is bound to the escape key.

backFromMenu() is called from

[java] public void play() {





which is in the class that handles the interactions with the xml document.

When I press the ‘play’ button, the above method is called and ‘play’ is written to sys.out. Now the problem is that although the nifty gui is removed, my original gui isn’t redrawn and I can no longer use the mouse do rotate the cam.

AND if I press escape again, the NullPointerException below is thrown.

(initCrossHairs() is the exact method from the tutorial.)


at de.lessvoid.nifty.screen.Screen.resetLayers(

at de.lessvoid.nifty.screen.Screen.endScreen(

at de.lessvoid.nifty.Nifty.exit(


It feels like I’ve messed something up. Something big. If the above code doesn’t explain the problem, I’ll post more.

Also I can’t seem to terminate the program properly. I’m trying






but I can’t get the application to close. Hope anyone can help with any of the issues :slight_smile:

Hi again.

I have now tried every combination of methods I can think of. I’ve read the nifty tutorials and I still don’t understand why the issues I listed above persist.

Please help. I’m in dire need - Any bit of help would be greately appreciated. :slight_smile:

Rather than completely exiting nifty when you exit your menu, you could have a completely blank nifty screen that it switches to instead (using gotoScreen() ). Chances are you’ll want to add other gui elements than the crosshairs to your main gui, and then you can just add them to the currently blank nifty screen.

My guess is something happens in nifty.exit() that causes the latter calls not to be executed.

The reason you get an NPE the second time is probably because nifty is not initialized then.

1 Like

Hey rickard.

It works like a charm, great idea. You really saved me a lot of hours there.

Thanks a lot man.

The way I do it is

[java] if(guiViewPort.getProcessors().contains(niftyDisplay))


niftyDisplay = new NiftyJmeDisplay(

assetManager, inputManager, audioRenderer, guiViewPort);

nifty = niftyDisplay.getNifty();

nifty.fromXml(“assets/Interface/gui.xml”, “start”);



which seems to work great. This could probably work well if refactored as a function where the xml document’s name was an argument.

There’s just one thing I haven’t figured out.

How do I properly terminate an application in jme3?

I tried the stop() method in the Application class, like it is done in the SimpleApplication class, but that doesn’t seem to do it for me.

Currently I’m using System.exit(0) but that seems like an ugly approach and the jmplatform doesn’t seem to like when I terminate applications like that. Perhaps I missed something in a tutorial somewhere.

This just seems like a minor thing though - my game is almost up and running.

Thanks for the great support so far everyone :slight_smile: