App is null when calling AppState method from Nifty

Hello. I’ve created an appstate - MenuState - and I believe it’s been initialised correctly. However, when I call a method from an associated Nifty screen, it throws a null exception when accessing the App.

Here’s how the app state is created in Main:

[java]

NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);

guiViewPort.addProcessor(niftyDisplay);

nifty = niftyDisplay.getNifty();

menuState = new MenuState();
stateManager.attach(menuState);
nifty.addXml(“Interface/Screens/start.xml”);
menuState.setEnabled(false);[/java]

Here’s how it’s initialised (I’ve just included initialize() and bind()):

[java]
public SimpleApplication app;
private Nifty nifty;
private Screen screen;

@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (SimpleApplication)app;
}

@Override
public void bind(Nifty nifty, Screen screen) {
this.nifty = nifty;
this.screen = screen;
}
[/java]

And when I call this MenuState function via a Nifty onClick interaction:

[java]
public void niftyMethod() {System.out.println("App ref: " + this.app); }
[/java]

I get “App ref: null”.

Have I created the app state correctly, and if so, how do I get access to the App?

EDIT:

I fixed it by altering my simpleInitApp() method thus:

[java]

NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);

nifty = niftyDisplay.getNifty();
nifty.addXml("Interface/Screens/start.xml");
nifty.addXml("Interface/Screens/game.xml");
	
menuState = (MenuState) nifty.getScreen("start").getScreenController();
stateManager.attach(menuState);
	
gameState = (GameState) nifty.getScreen("game").getScreenController();
stateManager.attach(gameState);
	
nifty.gotoScreen("start");
menuState.setEnabled(true);
	
guiViewPort.addProcessor(niftyDisplay);

[/java]

I assume this is the recommended way to register screen controllers when working with AbstractAppStates and individual Nifty XML files?

Actually, I’m still confused, unfortunately.

In my GameState class I have a method called startNew(), which loads the scene, objects, lights, fog etc. When switching to gameState to start a new game from the main menu, it works fine.

Often, I like to skip the menu and just load the gameState straight up. But when I call startNew(), I get a null exception when trying to access the app variable, which I though was set during the initialize() method when the screen controller was attached?

This was added to my simpleInitApp() from the post above:

[java]
if (skipMenu)
{
nifty.gotoScreen(“game”);
menuState.setEnabled(false);
gameState.setEnabled(true);
gameState.startNew();
}
else
{
nifty.gotoScreen(“start”);
menuState.setEnabled(true);
gameState.setEnabled(false);
}
[/java]

I’d be grateful for any help here. Thanks.

I can’t see why your way wouldn’t work with a quick scan through - however it’s not the recommended way anyway.

Create the screen controllers yourself and attach them as app states, then use nifty.registerScreenControllers before calling the addXml.

Fantastic, that did it. Thanks very much.

I’ve been a bit confused about when to call things and how Initialize() was used. Manually building, attaching and registering the controllers sorted everything out.

Yes, that’s how I always do it just because it means I have control over what’s going on rather than relying on anything else.