Nifty inside attachAppstate()

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?

Give us a stack trace, that will give us a better clue what might be causing this.

One thing to check would be whether your XML actually declared a “hud” screen. (Nifty is case sensitive, so watch out for that.)
But that’s just a shot in the dark, the stack trace will tell what operation exactly failed inside Nifty.

Here it is:

[java]
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker callMethod
WARNING: Exception: java.lang.reflect.InvocationTargetException
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke0 (null:-2)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke (null:-1)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.DelegatingMethodAccessorImpl invoke (null:-1)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.reflect.Method invoke (null:-1)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:108)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1166)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1144)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:353)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty update (Nifty.java:292)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme endInput (InputSystemJme.java:113)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager processQueue (InputManager.java:817)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager update (InputManager.java:881)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.Application update (Application.java:605)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.SimpleApplication update (SimpleApplication.java:230)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay runLoop (LwjglAbstractDisplay.java:151)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglDisplay runLoop (LwjglDisplay.java:185)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay run (LwjglAbstractDisplay.java:228)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.Thread run (null:-1)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: Root Cause: java.lang.NullPointerException
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: main.HudScreen stateAttached (HudScreen.java:66)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.state.AppStateManager attach (AppStateManager.java:126)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: main.PokerMain toHudScreen (PokerMain.java:135)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: main.StartScreen startGame (StartScreen.java:100)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke0 (null:-2)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke (null:-1)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.DelegatingMethodAccessorImpl invoke (null:-1)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.reflect.Method invoke (null:-1)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:108)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1166)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1144)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:353)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty update (Nifty.java:292)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme endInput (InputSystemJme.java:113)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager processQueue (InputManager.java:817)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager update (InputManager.java:881)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.Application update (Application.java:605)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.SimpleApplication update (SimpleApplication.java:230)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay runLoop (LwjglAbstractDisplay.java:151)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglDisplay runLoop (LwjglDisplay.java:185)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay run (LwjglAbstractDisplay.java:228)
Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.Thread run (null:-1)
Feb 19, 2013 4:16:28 PM com.jme3.renderer.lwjgl.LwjglRenderer cleanup
INFO: Deleting objects and invalidating state
Feb 19, 2013 4:16:28 PM com.jme3.scene.Node detachChildAt
INFO: Gui Node (Node): Child removed.
Feb 19, 2013 4:16:28 PM com.jme3.scene.Node detachChildAt
INFO: Gui Node (Node): Child removed.
Feb 19, 2013 4:16:28 PM com.jme3.scene.Node detachChildAt
INFO: Gui Node (Node): Child removed.
Feb 19, 2013 4:16:28 PM com.jme3.scene.Node detachChildAt
INFO: Gui Node (Node): Child removed.
Feb 19, 2013 4:16:28 PM com.jme3.input.lwjgl.LwjglMouseInput destroy
INFO: Mouse destroyed.
Feb 19, 2013 4:16:28 PM com.jme3.input.lwjgl.LwjglKeyInput destroy
INFO: Keyboard destroyed.
Feb 19, 2013 4:16:28 PM com.jme3.system.lwjgl.LwjglAbstractDisplay deinitInThread
INFO: Display destroyed.
[/java]

The HudScreen nifty field is null.
The content of this post is meant to be read as a straight information or question without an implicit dismissive stance or interest in having the other party feel offended unless there’s emotes that hint otherwise or there’s an increased use of exclamation marks and all-capital words.

@tsogtoo That’s not the stack trace, that’s the log.

Yes, it is. It is the one appeared in Console

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

is there. Isn’t it defining nifty? How do I define then?

@tsogtoo Sorry no, there’s no stack trace in the console output you posted.
Stack traces look like this:
[java]org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class ‘com.ibm.db2.jcc.DB2Driver’
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1136)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
at com.azurian.lce.usuarios.ConnectionManager.getConnection(ConnectionManager.java:65)
at com.azurian.lce.usuarios.db2.UsuarioDAOImpl.autenticar(UsuarioDAOImpl.java:101)
at com.azurian.lce.usuarios.UsuarioServiceImpl.autenticar(UsuarioServiceImpl.java:31)
at com.azurian.lce.web.admin.actions.LoginAction.execute(LoginAction.java:49)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: COM.ibm.db2.jcc.DB2Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1130)
… 23 more[/java]
(In case you’re wondering: the application that spit this out is entirely unrelated to jme or graphics programming.)

try registering the controller

@toolforger: Nifty “swallows” exceptions that happen in the callback methods, see lines 87/88 in the log:

[java]Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.Thread run (null:-1)[/java]

@tsogtoo: I don’t see those methods in the Hud class.

@normen
Actually, it is there. I just didn’t post it.
[java]
public class HudScreen extends AbstractAppState implements ScreenController{

private Nifty nifty;

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 bind(Nifty nifty, Screen screen) {
    this.nifty = nifty;
    this.screen = screen;
  }

public void stateAttached(AppStateManager stateManager){
	nifty.gotoScreen("hud");
	}

public void stateDetached(AppStateManager stateManager){

}

}
[/java]

Maybe you attach the state before it becomes the current screen and thus sets the nifty? At any instance, something is null there, find out what, its not so hard.
The content of this post is meant to be read as a straight information or question without an implicit dismissive stance or interest in having the other party feel offended unless there’s emotes that hint otherwise or there’s an increased use of exclamation marks and all-capital words.

A general rule:
If you are overriding stateAttached() in an app state then you are doing it wrong. Never override stateAttached() and stateDetached(). They do not do what you want.

Okay, @pspeed.
One more question: I toggled to “hud” screen from “start” screen. But the buttons of “hud” scrreen don’t interact when I click on it. Is this because “hud” screen’s controller class is not active?

Okay, I got it. I registered the controller class.

@normen said: @toolforger: Nifty "swallows" exceptions that happen in the callback methods, see lines 87/88 in the log:

[java]Feb 19, 2013 4:16:27 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.Thread run (null:-1)[/java]

Swallowing such exceptions is a major malpractice in my book.
Increasing the log level might help. The packages to configure logging for would be de.lessvoid.
Nifty uses Eventbus, which might be the actual culprit. In that case. the logging settings would be for org.bushe.swing.event.

Here’s an entirely different approach that I use on Eclipse (something equivalent should be available in the SDK, i.e. Netbeans): exception breakpoints.
They stop the debugger at the statement that throws the exception.
One of the best tools in my debugging toolbox:

  • You see the offending code right away.
  • If the offending code isn’t the real culprit, there’s a good chance it’s somewhere up the stack.
  • You’ll see the first point in time where the exception happens, not some later followup error.
    Of course, if the null value was passed through a gazillion of data structures before finally being dereferenced, it can be hard to trace its origin. All methods have their limits :-/

yeh, this feature also exists in the JMEDK. Debug -> New BreakPoint -> Exception

You can filter out specific classes, exceptions, and skip ones you are already catching as well (I’ve not used the Eclipse one, so I can’t comment how it compares)

Eclipse can filter exception subclasses and by whether the exception is caught or not.
It cannot filter by package/class that the exception originates in, or would be caught in. Not without using conditional breakpoints anyway. If Netbeans can do that, then that’s a point where Netbeans is better.