SimpleApplication members are null in an AbstractAppAState

So I am trying to make use of app states for the first time. I have been reading documentation and Googling answers to questions along the way. However I am at a point now where I can’t seem to find anything in the documentation or Google to help with this issue. I cannot figure out why some of these variables are null.

I have implemented an AbstractAppState and here is the initialize method:
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);

    BulletAppState bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);

    mSimpleApplication = (Main) app;
    mSimpleApplication.getFlyByCamera().setMoveSpeed(100); // the flyByCam is null
    mCamera = mSimpleApplication.getCamera(); // cam is also null along with assetManager and many others
    Node rootNode = mSimpleApplication.getRootNode();
    ...
}

in the Main class that extends SimpleApplication i start it like this:
AppState bridgeState = new BridgeState();
getStateManager().attach(bridgeState);
bridgeState.initialize(getStateManager(), this);
bridgeState.setEnabled(true);

Do NOT call this method yourself. Not a single tutorial would ever have done this.

And the javadoc is pretty clear on when this is supposed to be called:
http://javadoc.jmonkeyengine.org/com/jme3/app/state/AbstractAppState.html#initialize-com.jme3.app.state.AppStateManager-com.jme3.app.Application-

ie: by the AppStateManager.

Create it. Set it up how you like it (setEnabled() or whatever) then attach it. That’s it.

Hi thanks for getting back to me. Yes I guess the docs don’t say to actually call the method just says to initialize it. I guess I figured that’s what it meant. Although I think I tried it without calling initialize myself and initialize was not being called. I’ll play with it a little more, remove that call and see what happens.

It says right there in the docs what calls it:

Note: you said you read the docs but must have skipped the tutorials that included app states. I do recommend doing as many of the tutorials as you can stomach.

I think I have it running mostly however in the UI the player will click a button to load a level. In the method that is called when the player presses a button I do this:

BridgeState bridgeState = getStateManager().getState(BridgeState.class);
bridgeState.setEnabled(true);

However bridgeState is null.

Then it was never attached.

Even with this?

public void simpleInitApp() {
    mNiftyJmeDisplay = new NiftyJmeDisplay(getAssetManager(), getInputManager(), getAudioRenderer(), getGuiViewPort());
    mNifty = mNiftyJmeDisplay.getNifty();
    
    getGuiViewPort().addProcessor(mNiftyJmeDisplay);        
    getFlyByCamera().setDragToRotate(true);
    
    showScreen("start");
    BridgeState bridgeState = new BridgeState();
    getStateManager().attach(bridgeState);
}

I have attached a debugger and went through the AppStateManager and everything seems fine, BridgeState gets added to the initializing list, and then gets transferred to the states list. But when I call getState it returns null and the states list is completely empty.

Do you somewhere create your own AppStateManager?

No I don’t have a custom AppStateManager.

States are initialized before the simpleInit is called.
If you try to access this state from another appstate by doing stateManager.getState(bla…) It’s not yet there.
The best way to do it is to pass all needed appstates in the application constructor:

public myApp(){
    super(allMy, appSates, inThe, orderIWant, themTo, initialize);
}

then you’ll be able to access any of them from the simpleInit of the application and you’ll be able to access all previous states in the list from the initialize of one of them.

EDIT: Actually I’m wrong about the fact that they are initialized before the simpleInit, they are actually initialized the next frame they’ve been attached.
Anyway, the remark on the state order still stands.

Where do you call this code?

Or if I’m going to answer like you post:
“re dor you ca” ie: only giving a tiny bit of information that is not fully useful on its own.

More info is better if you want your problem solved. Else you can keep playing “20 questions” with the crowd.

Any time AFTER stateManager.attach(myState) is called then it will be available to getState(). So if getState() is null then that code is called before you attached. 1000000% guaranteed.

I attach it in simpleInit and I don’t access it with getState until the player clicks a button in the ui.

Then where that’s clicking is not setup properly.

And probably you didn’t report the error correctly.

When you say this:
However bridgeState is null.

…did you really mean that you got a NullPointerException near there?

Because a super-duper-super-super-super (almost always) common thing for new nifty users is to not setup their UI controllers properly and have no access to anything JME related.

But I can guarantee with absolute certainty that if you call stateManger.getState(SomeClass.class) at ANY TIME AFTER_ calling stateManager.attach(new SomeClass()); that it WILL NOT RETURN NULL.

So your problem is somewhere else. Probably in nifty setup now I’m guessing. I don’t know what getStateManager() lives on where you are calling it but are you sure it’s not returning null?

At this point, the problem is clearly on your end in the setup somewhere. So you will have to debug that on your end either in the debugger or by adding logging and so on.

The only way any of us can help further is if you reduce it to a single class test case that illustrates the problem. Then we’ll be able to more easily point to what you did wrong.

Otherwise, we’re just going to keep playing this back and forth as we slowly get closer to the part of your code that’s not working right.