Atach a appstate from another appstate

Hi everybody! What im trying to do is to attach an appstate from another appstate but it bring me an nullpointer exception.

My code is something like this:

[java]

public void goArcadeState() {
stateManeger.detach(this); //Detach he active state
ArcadeState Arcade = new ArcadeState (this.app, this.nifty); //Create a AcadeState to atachit,
stateManager.attach(ArcadePlayerSelect); //This is where i get nullpointer :S
}
[/java]

This method is called by a nifty button. What im doing wrong? Wich is the best way of doing this?

Thank you all!

Well your variable is differently named in 3 and 4, according to the comments this makes no sense.

And btw, methods and variables in java start with lowercase letters by convention, it makes the code easier to read.

Sorry i copied the code wrong. This is the code:

[java]
public void goArcadeMode() {
ArcadeState arcade = new ArcadeState(this.app, this.nifty);
stateManager.attach(arcade);
stateManeger.detach(this);
} [/java]

the error is:

java.lang.NullPointerException
at ScreenControllers.ControllerMenuPrincipal.goArcadeMode(ControllerMenuPrincipal.java:145)// the line3 in the code ive put here.

Looks like you are using an unintialized application. Like maybe you extended your class from SimpleApplication thinking you can access things like the assetManager etc. then, as if Java was a script language like PHP.

1 Like
@normen said: Looks like you are using an unintialized application. Like maybe you extended your class from SimpleApplication thinking you can access things like the assetManager etc. then, as if Java was a script language like PHP.

The class im calling is this:

[java]
public class ArcadeState extends AbstractAppState implements ScreenController
[/java]

Im calling goArcadeMode() from an appstate , could this be the cause of the problem?

stateMgr is null. You’ve never set it to anything, I guess.

Here is the part where I suggest that it is hard to learn Java and 3D game programming at the same time and then everyone gets upset. But it’s true.

2 Likes
@pspeed said: stateMgr is null. You've never set it to anything, I guess.

Here is the part where I suggest that it is hard to learn Java and 3D game programming at the same time and then everyone gets upset. But it’s true.

Yes the problem is that. How can i solve it?

If you use an AppState, only when initialize() is called you have the reference to the stateManager instance.

1 Like
@normen said: If you use an AppState, only when initialize() is called you have the reference to the stateManager instance.

Thank you to all i finally resolve it! I only had to make a this.stateManager = stateManager; in the initialize method.

Thanks all!