How to access AbstractAppState with nifty?

Hey Guys. I’ve searched the forum, but can’t find answer to this question. How can I access abstract app state variables (say stateManager) in order to manipulate game states with GUI? At the moment I register my screen controllers - everything works fine to a moment where I actually have to take user away from GUI interface to actual game - how can I do that? This is the code that I use. You can clearly see I’m extending AbstractAppState so I should have access to global vars no? Don’t worry about the comments - they’re just pointers for the optimization that will happen later on.

My main Class:

[java]package simulation;


import graphics.Scene;

import graphics.Train;


import java.util.logging.Level;

import java.util.logging.Logger;

import views.Map;

import views.StartScreen;

public class Simulation extends SimpleApplication


StartScreen startScreen = new StartScreen();

public static void main(String[] args)


Simulation app = new Simulation();






public void simpleInitApp()







StartScreen Class:

[java]package views;





import com.jme3.asset.AssetManager;

import com.jme3.bullet.BulletAppState;

import com.jme3.input.InputManager;

import com.jme3.renderer.ViewPort;

import com.jme3.scene.Node;

import de.lessvoid.nifty.Nifty;

import de.lessvoid.nifty.NiftyEventSubscriber;

import de.lessvoid.nifty.controls.ListBox;

import de.lessvoid.nifty.controls.ListBoxSelectionChangedEvent;

import de.lessvoid.nifty.elements.Element;


import de.lessvoid.nifty.elements.render.ImageRenderer;

import de.lessvoid.nifty.elements.render.PanelRenderer;

import de.lessvoid.nifty.elements.render.TextRenderer;

import de.lessvoid.nifty.screen.Screen;

import de.lessvoid.nifty.screen.ScreenController;


import java.util.List;

import models.LinesDB;

public class SimulationLinesScreen extends AbstractAppState implements ScreenController


private SimpleApplication app;

private Node rootNode;

private AssetManager assetManager;

private AppStateManager stateManager;

private InputManager inputManager;

private ViewPort viewPort;

private ViewPort guiViewPort;

private BulletAppState bullet;

private Nifty nifty;

private Screen screen;

// simulationLinesScreen

private LinesDB lineDB = new LinesDB();

private PanelRenderer lineNameBackground;

private TextRenderer lineName;

private ImageRenderer lineImage;

private TextRenderer lineDescription;


public void initialize(AppStateManager stateManager, Application app) {

super.initialize(stateManager, app); = (SimpleApplication) app; // can cast Application to something more specific

this.rootNode =;

this.assetManager =;

this.stateManager =;

this.inputManager =;

this.viewPort =;

this.bullet = this.stateManager.getState(BulletAppState.class);

this.guiViewPort =;


public void bind(Nifty nifty, Screen screen) {

this.nifty = nifty;

this.screen = screen;



public void onStartScreen() {


public void onEndScreen() {



public void update(float tpf) {


// Screen listeners


public void onBackToStartScreenClick(String id, NiftyMousePrimaryClickedEvent event) {




public void onStartSimulationParametersClick(String id, NiftyMousePrimaryClickedEvent event) {




public void onListBoxSelectionChanged(final String id, final ListBoxSelectionChangedEvent<String> event) {



Element lineNameElement = this.screen.findElementByName(“lineName”);

lineName = lineNameElement.getRenderer(TextRenderer.class);

Element lineImageElement = this.screen.findElementByName(“lineImage”);

lineImage = lineImageElement.getRenderer(ImageRenderer.class);

Element lineDescriptionElement = this.screen.findElementByName(“lineDesc”);

lineDescription = lineDescriptionElement.getRenderer(TextRenderer.class);

Element lineNameBackgroundElement = this.screen.findElementByName(“lineNameBackground”);

lineNameBackground = lineNameBackgroundElement.getRenderer(PanelRenderer.class);

// END

List<String> selection = event.getSelection();

for (String selectedItem : selection) {


lineNameBackground.setBackgroundColor(new Color(this.lineDB.getLineByName(selectedItem).getLineColor()));


lineImage.setImage(this.nifty.getRenderEngine().createImage(this.lineDB.getLineByName(selectedItem).getImage(), false));



private void setUndergroundList() {

ListBox linesList = nifty.getScreen(“simulationLinesScreen”).findNiftyControl(“linesList”, ListBox.class);

for (int i=0;i<lineDB.getLinesDB().size();i++) {






I’m probably missing something obvious but please bear with me - I’m just starting my work with jMonkey. Any help much appreciated!

Have you registered the app state as a screen controller with nifty? (Either before or at the same time as loading the Xml).

I’ve tried that zarch, but whenever I’m trying to load something (say, with a click of a button) I’m constantly getting null pointer exception - how come? It seems like nifty gui components can’t access anything from jme vars? help me here…?

Have you tried breakpointing the event callback thread and looking to see what its situation is? Could also check that its being called in the same instance of the controller that you registered…

Which variable is triggering the null pointer and why is that variable not being initialized? What is your stack trace?

I got this every time - I seriously don’t know what the problem is. I’m instancing my variables correctly - according to tuts…


WARNING: failed to invoke method [public void views.StartScreen.onStartNewSimulationClick(java.lang.String,] with Exception [null][java.lang.NullPointerException]


at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(

at sun.reflect.DelegatingMethodAccessorImpl.invoke(

at java.lang.reflect.Method.invoke(

at de.lessvoid.nifty.NiftyEventAnnotationProcessor$Subscriber.onEvent(

at org.bushe.swing.event.ThreadSafeEventService.publish(

at org.bushe.swing.event.ThreadSafeEventService.publish(

at de.lessvoid.nifty.Nifty.publishEvent(

at de.lessvoid.nifty.elements.PrimaryClickMouseMethods.publishEvent(

at de.lessvoid.nifty.elements.PrimaryClickMouseMethods.onClick(

at de.lessvoid.nifty.elements.ElementInteractionClickHandler.onClickMouse(

at de.lessvoid.nifty.elements.ElementInteractionClickHandler.process(

at de.lessvoid.nifty.elements.ElementInteraction.process(

at de.lessvoid.nifty.elements.Element.mouseEvent(

at de.lessvoid.nifty.screen.MouseOverHandler.processMouseEvent(

at de.lessvoid.nifty.screen.Screen.forwardMouseEventToLayers(

at de.lessvoid.nifty.screen.Screen.mouseEvent(

at de.lessvoid.nifty.Nifty.forwardMouseEventToScreen(

at de.lessvoid.nifty.Nifty.access$1400(

at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processEvent(

at de.lessvoid.nifty.Nifty$NiftyInputConsumerImpl.processMouseEvent(

at com.jme3.niftygui.InputSystemJme.onMouseButtonEventQueued(

at com.jme3.niftygui.InputSystemJme.forwardEvents(

at de.lessvoid.nifty.Nifty.update(

at com.jme3.niftygui.InputSystemJme.endInput(

at com.jme3.input.InputManager.processQueue(

at com.jme3.input.InputManager.update(



at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(



Caused by: java.lang.NullPointerException

at views.StartScreen.onStartNewSimulationClick(

… 33 more


Perhaps I should hit it from different angle - let me ask you this - what is the common procedure of switching between nifty and game state? Let’s say I click a button and it will switch to a background with a blue cube (like the first tutorial cube)?

Ahhhh I’m so stupid! I forgot about attaching my main appState to stateManager beforehand!