So here it is. I have 3 classes: Main, StartScreen, HudScreen;
Main class contains following 2 methods:
[java]
public class PokerMain extends SimpleApplication {
static StartScreen SS;
static HudScreen HS ;
public void simpleInitApp() {
SS=new StartScreen(this);
HS= new HudScreen(this);
getStateManager().attach(SS);
}
public void toStartScreen(){
getStateManager().detach(HS);
getStateManager().attach(SS);
}
public void toHudScreen(){
getStateManager().detach(SS);
getStateManager().attach(HS);
}
[/java]
So as you can see, main method attaches first StartScreen (which is appstate as well as controller class, which controls Start screen in xml) .
Start screen appears on the window. It has a button “continue”. When this button is clicked, StartGame() method of this controller class is called as shown below, which in turn calls toHudScreen() method of Main class shown above:
[java]
public class StartScreen extends AbstractAppState implements ScreenController {
public StartScreen(SimpleApplication app) {
/** Your custom constructor, can accept arguments */
this.app=app;
this.rootNode = app.getRootNode();
this.guiViewPort = app.getGuiViewPort();
this.guiNode = app.getGuiNode();
this.assetManager = app.getAssetManager();
this.inputManager= app.getInputManager();
this.audioRenderer= app.getAudioRenderer();
this.stateManager= app.getStateManager();
}
public void startGame() {
pk=new PokerMain();
pk.toHudScreen();
}
public void stateAttached(AppStateManager stateManager){
}
public void stateDetached(AppStateManager stateManager){
}
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
inputManager,
audioRenderer,
guiViewPort);
nifty = niftyDisplay.getNifty();
nifty.fromXml("/Xml/Screen.xml", "start");
guiViewPort.addProcessor(niftyDisplay);
}
}
[/java]
When the toHudScreen() method of Main class is called, it detaches StartSreen and attaches HudSreen. And it is supposed to change the gui from start sreen to hud sreen. The code of HudSreen class is shown below.
[java]
public class HudScreen extends AbstractAppState implements ScreenController{
public HudScreen(SimpleApplication app){//
this.app=app;
this.rootNode = app.getRootNode();
this.guiViewPort = app.getGuiViewPort();
this.guiNode = app.getGuiNode();
this.assetManager = app.getAssetManager();
this.inputManager= app.getInputManager();
this.audioRenderer= app.getAudioRenderer();
this.stateManager= app.getStateManager();
}
public void stateAttached(AppStateManager stateManager){
nifty.gotoScreen("hud");
}
public void stateDetached(AppStateManager stateManager){
}
}
[/java]
So I tried to display hud sreen when HudSreen state is attached. So I put nifty.gotoSreen(“hud”) line inside stateAttached() method.
It gives exception java.lang.NullPointerException.
My question is why it gives this exception? Is it because nifty doesn’t have correct reference?