Designing Nifty GUI with Java without getting InvocationTargetException

So I’m trying to create a menu in Nifty GUI and I can se all the panels and I can call methods. However when I app trying to call methods that contain code such as nifty.goToScreen(); or to close my app I get an error. I have seen some posts about this problem but none of them shows how you solve it when doing the design in Java code. This is what I have.

My Controller class…

package mygame;

import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.builder.LayerBuilder;
import de.lessvoid.nifty.builder.PanelBuilder;
import de.lessvoid.nifty.builder.ScreenBuilder;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import edu.cth.mosquito.main.MosquitoSimulator;
import java.util.List;


public class MenuController extends AbstractAppState implements ScreenController{

private Main rootApp;
private NiftyJmeDisplay ourScreen;
private Nifty nifty;

@Override
public void initialize(AppStateManager asm, Application app){
    
    super.initialize(asm, app);
    rootApp = (SimpleApplication)app;
    
    ourScreen = new NiftyJmeDisplay(app.getAssetManager(), app.getInputManager(), app.getAudioRenderer(), app.getGuiViewPort());
   
    nifty = ourScreen.getNifty();
    
    app.getGuiViewPort().addProcessor(ourScreen);
    
    nifty.addScreen("start", new ScreenBuilder("NiftyScreen"){{
        controller(new mygame.MenuController());
        
        layer(new LayerBuilder("background"){{
            childLayoutVertical();
        
        }});
        
        layer(new LayerBuilder("foreground"){{
            childLayoutCenter();
            
            panel(new PanelBuilder("start"){{
                childLayoutCenter();
                width("100%");
                height("100%");
                backgroundColor("#ffff");
                visibleToMouse(true);
                interactOnClick("exitGame()");
                
            }});
        }});
    
    }}.build(nifty));
    
    
    nifty.gotoScreen("start");
    
}

public void cleanUp(){
    nifty.exit();
}

@Override
public void bind(Nifty nifty, Screen screen) {
    
}

@Override
public void onStartScreen() {
    
}

@Override
public void onEndScreen() {
    
}

public void startGame(){
    
   nifty.exit();
    
}

public void switchScreen(String id){
    
    nifty.gotoScreen(id);
    
}

public void exitGame(){
    
    rootApp.stop();
    
}
}

My Main class…

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material; 
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

public class Main extends SimpleApplication {

public static void main(String[] args) {
    Main m = new Main();
    m.start();
}

private Geometry geom;
private Geometry geom2;
private MenuController menu = new MenuController();

@Override
public void simpleInitApp() {
    
    stateManger.attach(menu);
    
    Box b = new Box(1, 1, 1);
    geom = new Geometry("myBox", b);
    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat.setColor("Color", ColorRGBA.Blue);
    geom.setMaterial(mat);
    
    Box ourBox = new Box(1, 1, 1);
    geom2 = new Geometry("ourBox", ourBox);
    Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
    mat2.setColor("Color", ColorRGBA.Green);
    geom2.setLocalTranslation(4, 0, 4);
    geom2.setMaterial(mat2);
    
    rootNode.attachChild(geom2);
    rootNode.attachChild(geom);

    
}

@Override 
public simpleUpdate(float tpf){


}
}

When I for example press the panel to exit the game I get this error…

maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker callMethod
Varning: Exception: java.lang.reflect.InvocationTargetException
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: sun.reflect.NativeMethodAccessorImpl invoke0 (NativeMethodAccessorImpl.java:-2)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: sun.reflect.NativeMethodAccessorImpl invoke (NativeMethodAccessorImpl.java:57)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: sun.reflect.DelegatingMethodAccessorImpl invoke (DelegatingMethodAccessorImpl.java:43)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: java.lang.reflect.Method invoke (Method.java:606)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:104)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1174)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1152)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:354)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty access$1700 (Nifty.java:77)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processEvent (Nifty.java:1374)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processMouseEvent (Nifty.java:1329)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.niftygui.InputSystemJme handleMouseEvent (InputSystemJme.java:124)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.niftygui.InputSystemJme onMouseButtonEventQueued (InputSystemJme.java:232)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.niftygui.InputSystemJme forwardEvents (InputSystemJme.java:296)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty update (Nifty.java:288)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.niftygui.InputSystemJme endInput (InputSystemJme.java:113)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.input.InputManager processQueue (InputManager.java:819)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.input.InputManager update (InputManager.java:883)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.app.Application update (Application.java:604)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.app.SimpleApplication update (SimpleApplication.java:231)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.system.lwjgl.LwjglAbstractDisplay runLoop (LwjglAbstractDisplay.java:151)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.system.lwjgl.LwjglDisplay runLoop (LwjglDisplay.java:185)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.system.lwjgl.LwjglAbstractDisplay run (LwjglAbstractDisplay.java:228)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: java.lang.Thread run (Thread.java:744)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: Root Cause: java.lang.NullPointerException
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: edu.cth.mosquito.controller.MenuController startGame (MenuController.java:106)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: sun.reflect.NativeMethodAccessorImpl invoke0 (NativeMethodAccessorImpl.java:-2)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: sun.reflect.NativeMethodAccessorImpl invoke (NativeMethodAccessorImpl.java:57)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: sun.reflect.DelegatingMethodAccessorImpl invoke (DelegatingMethodAccessorImpl.java:43)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: java.lang.reflect.Method invoke (Method.java:606)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.NiftyMethodInvoker callMethod (NiftyMethodInvoker.java:145)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.NiftyMethodInvoker performInvoke (NiftyMethodInvoker.java:104)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty$DelayedMethodInvoke perform (Nifty.java:1174)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty invokeMethods (Nifty.java:1152)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty handleDynamicElements (Nifty.java:354)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty access$1700 (Nifty.java:77)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processEvent (Nifty.java:1374)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl processMouseEvent (Nifty.java:1329)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.niftygui.InputSystemJme handleMouseEvent (InputSystemJme.java:124)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.niftygui.InputSystemJme onMouseButtonEventQueued (InputSystemJme.java:232)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.niftygui.InputSystemJme forwardEvents (InputSystemJme.java:296)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: de.lessvoid.nifty.Nifty update (Nifty.java:288)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.niftygui.InputSystemJme endInput (InputSystemJme.java:113)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.input.InputManager processQueue (InputManager.java:819)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.input.InputManager update (InputManager.java:883)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.app.Application update (Application.java:604)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.app.SimpleApplication update (SimpleApplication.java:231)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.system.lwjgl.LwjglAbstractDisplay runLoop (LwjglAbstractDisplay.java:151)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.system.lwjgl.LwjglDisplay runLoop (LwjglDisplay.java:185)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: com.jme3.system.lwjgl.LwjglAbstractDisplay run (LwjglAbstractDisplay.java:228)
maj 25, 2015 9:25:42 EM de.lessvoid.nifty.NiftyMethodInvoker logException
Varning: java.lang.Thread run (Thread.java:744).

So is there a solution to this problem?

Thanks,

There is your real error.

Here is a quote from another post that did the design in XML and he got basically the same error message. And the reply he got was the following;

"I think
“jmedisplay.getNifty().fromXml(“Interface/mainmenuscreen.xml”, “MainMenu”);” is not right. It should be
“jmedisplay.getNifty().fromXml(“Interface/mainmenuscreen.xml”, “MainMenu”, this);”

See the notice in the middle of this page: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:nifty_gui_scenarios1

Otherwise Nifty doesn’t know which screen controller instance to call so it will create a new instance of MainMenuState."

Is it not the exact same problem here aswell?

That’s your problem. You create a new instance of the thing but it’s not attached to the state manager so of course it doesn’t have any of the stuff you want… but that would be bad anyway as then it would instantiate itself again and again.

Your structure is a little bit odd, actually… but at the very least you should be passing your actual menu controller and not creating a new one.

Nifty is very unforgiving to newish Java developers.

Okey now I solved it thanks man!