Nifty onClick interact not working

I am writing a main menu, but I got stuck at the first button action… I want a quit() method to be called when the button “Exit” is pressed. But it just doesn’t work…

Heres my MainMenu.xml:
<?xml version=“1.0” encoding=“UTF-8” standalone=“yes”?>
<nifty>
<useControls filename=“nifty-default-controls.xml”/>
<useStyles filename=“nifty-default-styles.xml”/>
<screen id=“start” controller=“mw.appstates.StartScreenState”>
<layer id=“layer” backgroundColor="#003f" childLayout=“vertical”>
<panel id=“panel” visibleToMouse=“true” height=“350px” width=“1300px” align=“center” childLayout=“center” valign=“center”>
<effect>
<onStartScreen name=“move” mode=“in” direction=“top” length=“300” startDelay=“0” inherit=“true”/>
<onEndScreen name=“move” mode=“out” direction=“bottom” length=“300” startDelay=“0” inherit=“true”/>
</effect>
<image id=“title” height=“300px” width=“1300px” filename=“Interface/GUI/res/title3.png”/>
</panel>
<panel id=“GPanel1” height=“30%” width=“50%” align=“center” childLayout=“vertical”>
<effect>
<onStartScreen name=“move” mode=“in” direction=“top” length=“300” startDelay=“0” inherit=“true”/>
<onEndScreen name=“move” mode=“out” direction=“bottom” length=“300” startDelay=“0” inherit=“true”/>
</effect>
<control name=“label” id=“spacing” text="" height=“170px” font=“aurulent-sans-16.fnt” width=“100px”/>
<control name=“button” id=“playsp” height=“37px” width=“325px” align=“center” label=“Campagin” childLayout=“center”/>
<control name=“label” id=“spacing2” text="" height=“40px” font=“aurulent-sans-16.fnt” width=“100px”/>
<control name=“button” id=“playmp” height=“37px” width=“325px” align=“center” label=“Multiplayer” childLayout=“center”/>
<control name=“label” id=“spacing3” text="" height=“40px” font=“aurulent-sans-16.fnt” width=“100px”/>
<control name=“button” id=“settings” height=“37px” width=“325px” align=“center” label=“Settings” childLayout=“center”/>
<control name=“label” id=“spacing4” text="" height=“40px” font=“aurulent-sans-16.fnt” width=“100px”/>
<control name=“button” id=“exit” height=“37px” width=“325px” align=“center” label=“Exit” visibleToMouse=“true”>
<interact onClick=“quit()”/> <— this is not working
</control>
</panel>
</layer>
</screen>
</nifty>

mw.appstates.StartScreenState class:

[java]public class StartScreenState extends AbstractAppState implements ScreenController{
private Nifty nifty;
private Screen screen;
private SimpleApplication app;

@Override
public void initialize(AppStateManager stateManager, Application app) {
    super.initialize(stateManager, app);
    this.app = (SimpleApplication)app;
    //TODO: initialize your AppState, e.g. attach spatials to rootNode
    //this is called on the OpenGL thread after the AppState has been attached
}

@Override
public void update(float tpf) {
    //TODO: implement behavior during runtime
}

@Override
public void cleanup() {
    super.cleanup();
    //TODO: clean up what you initialized in the initialize method,
    //e.g. remove all spatials from rootNode
    //this is called on the OpenGL thread after the AppState has been detached
}

public void bind(Nifty nifty, Screen screen) {
    this.nifty = nifty;
    this.screen = screen;
}

public void onStartScreen() {
    //throw new UnsupportedOperationException("Not supported yet.");
    Element niftyElement = nifty.getCurrentScreen().findElementByName("exit");
    niftyElement.getElementInteraction().getPrimary().setOnClickMethod(new NiftyMethodInvoker(nifty, "quit()", this));
}

public void onEndScreen() {
    //throw new UnsupportedOperationException("Not supported yet.");
}

public final void quit(){ <strong>&lt;--- The void not working</strong>
    app.stop();
}

}[/java]

Also i get the following stack trace when i click a button:
[java]WARNING: Exception: java.lang.reflect.InvocationTargetException
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke0 (NativeMethodAccessorImpl.java:-2)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke (NativeMethodAccessorImpl.java:57)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.DelegatingMethodAccessorImpl invoke (DelegatingMethodAccessorImpl.java:43)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.reflect.Method invoke (Method.java:606)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:104)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1174)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1152)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:354)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty access$1700 (Nifty.java:77)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processEvent (Nifty.java:1374)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processMouseEvent (Nifty.java:1329)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme handleMouseEvent (InputSystemJme.java:124)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme onMouseButtonEventQueued (InputSystemJme.java:197)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme forwardEvents (InputSystemJme.java:261)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty update (Nifty.java:288)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme endInput (InputSystemJme.java:113)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager processQueue (InputManager.java:819)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager update (InputManager.java:883)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.Application update (Application.java:604)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.SimpleApplication update (SimpleApplication.java:231)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay runLoop (LwjglAbstractDisplay.java:151)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglDisplay runLoop (LwjglDisplay.java:185)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay run (LwjglAbstractDisplay.java:228)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.Thread run (Thread.java:744)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: Root Cause: java.lang.NullPointerException
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: mw.appstates.StartScreenState quit (StartScreenState.java:59)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke0 (NativeMethodAccessorImpl.java:-2)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.NativeMethodAccessorImpl invoke (NativeMethodAccessorImpl.java:57)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: sun.reflect.DelegatingMethodAccessorImpl invoke (DelegatingMethodAccessorImpl.java:43)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.reflect.Method invoke (Method.java:606)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:104)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1174)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1152)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:354)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty access$1700 (Nifty.java:77)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processEvent (Nifty.java:1374)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processMouseEvent (Nifty.java:1329)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme handleMouseEvent (InputSystemJme.java:124)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme onMouseButtonEventQueued (InputSystemJme.java:197)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme forwardEvents (InputSystemJme.java:261)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: de.lessvoid.nifty.Nifty update (Nifty.java:288)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.niftygui.InputSystemJme endInput (InputSystemJme.java:113)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager processQueue (InputManager.java:819)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.input.InputManager update (InputManager.java:883)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.Application update (Application.java:604)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.app.SimpleApplication update (SimpleApplication.java:231)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay runLoop (LwjglAbstractDisplay.java:151)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglDisplay runLoop (LwjglDisplay.java:185)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: com.jme3.system.lwjgl.LwjglAbstractDisplay run (LwjglAbstractDisplay.java:228)
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: java.lang.Thread run (Thread.java:744)[/java]

I’m a bit green in this whole thing, so please help me.

I’m not sure that this is the problem but yuo are having a null pointer exception as root cause :
[java]
WARNING: Root Cause: java.lang.NullPointerException
jun 12, 2014 1:29:00 PM de.lessvoid.nifty.NiftyMethodInvoker logException
WARNING: mw.appstates.StartScreenState quit (StartScreenState.java:59)
[/java]

Could you debug and see that the quit method is actually called ?

The screen controller app state was never attached to the state manager.

Either you haven’t attached it or you haven’t given the attached instance to nifty and it is creating its own unattached version.

1 Like

That worked, thanks.