Nifty and AppStates

@Screamconjoiner said: I tried that, but it didn't work. While debugging, breaking the calls down line by line as suggested by @sgold, I found that the NPE is being caused by the getStateManager() method call. I don't understand why, would you guys know of a reason as to why this would happen?

Because app is null.

This line loads the XML and instantiates any controllers that haven’t been registered yet:
nifty.fromXml(“Interface/newNiftyGui.xml”, “start”, Start);

This line registers a controller that won’t be used because it was already instantiated in the line above:
nifty.registerScreenController(Start);

Edit: actually, I see that you are passing it in… so I don’t know. Nifty can be tricky.

1 Like

Log something in all of the methods of your controller/state… including the constructor. That will help tell you what is happening.

1 Like

I got it to work!

Main.java
[java]
public void startGame() {

    GameState = new GamePlayAppState();
    NiftyState = new NiftyStart();
    PauseState = new PauseState();
    OverState = new GameOverAppState();
    InputState = new GamePlayInput();
    GameState.setEnabled(true);
    
    NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
                                                      inputManager,
                                                      audioRenderer,
                                                      guiViewPort);
    Nifty nifty = niftyDisplay.getNifty();

    
    guiViewPort.addProcessor(niftyDisplay);
    inputManager.setCursorVisible(true);
    

    stateManager.attach(GameState);
    stateManager.attach(NiftyState);
    stateManager.attach(InputState);
    nifty.registerScreenController(NiftyState);
    nifty.fromXml("Interface/newNiftyGui.xml", "start");

[/java]

I registered the screen controller before creating the Nifty window from XML. Perhaps that is what you were suggesting @pspeed, but I was unable to follow. Thank you both very much for the (incredibly prompt AND very useful) help! I would not have thought to debug line by line in the IDE, as I generally work by command line and Emacs and was not familiar with how debugging worked the jME IDE =)

I’m glad we were able to help.

Learning to use the IDE’s debugger would be a worthwhile investment of effort. I think you’ll find the debugger even more useful than this forum.