# Nifty popup fails to register

I'm new to Nifty GUI, so I'm following a tutorial here for making popups. For now, I'm just trying to get a very basic "test" popup to show, but I get multiple errors and none of them make much sense. To show a popup, I believe it is necessary to first have a Nifty Screen already showing, which I do. So here is the ScreenController for the working Nifty Screen:

public class WorkingScreen extends AbstractAppState implements ScreenController {

//Main is my jme SimpleApplication
private Main app;
private Nifty nifty;
private Screen screen;

public WorkingScreen() {}

public void equip(String slotstr) {
int slot = Integer.valueOf(slotstr);
System.out.println("Equipping item in slot "+slot);
//Here's where it STOPS working.
app.getPlayer().registerPopupScreen(nifty);
System.out.println("Registered new popup");
Element ele = nifty.createPopup(app.getPlayer().POPUP);
System.out.println("popup is " +ele);
nifty.showPopup(nifty.getCurrentScreen(), ele.getId(), null);
}

@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (Main)app;
}

@Override
public void update(float tpf) {
/** jME update loop! */
}

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


When I call equip(0) the system prints Equipping item in slot 0, then a lot of errors and none of the subsequent println()'s. Clearly it botches somewhere in Player.registerPopupScreen(Nifty nifty). Here's the method:

public final String POPUP = "Test Popup";
public void registerPopupScreen(Nifty nifty) {
System.out.println("Attempting new popup");
PopupBuilder b = new PopupBuilder(POPUP) {{
childLayoutCenter();
backgroundColor("#000a");

panel(new PanelBuilder() {{
id("List");
childLayoutCenter();
height(percentage(75));
width(percentage(50));
control(new ButtonBuilder("TestButton") {{
label("TestButton");
width("120px");
height("40px");
align(Align.Center);
}});
}});
}};
System.out.println("PopupBuilder success.");
b.registerPopup(nifty);
System.out.println("Registerpopup success.");
}


Because that first println() doesn't show, it looks like this method isn't even called at all!

# Edit

After removing all calls on the Player object, the popup works. It seems I'm not "allowed" to access the player from the ScreenController. Unfortunate, since I need information on the player for the popup. Is there a workaround?

• +1 for a great question title. When taken outside of the jME / games context, it could mean just about anything. – Engineer Nov 7 '11 at 21:47
• lol.. I do plan on it being "nifty" as well :p – Snailer Nov 7 '11 at 22:00
• @Snailer Would you like me to migrate this to Stack Overflow? You might get an answer there. Either way, I strongly suggest you bring up this topic in the jME or Nifty GUI forums (or both); it appears to be a bit too specific for anyone here to answer. – Ricket Nov 9 '11 at 17:46
• I think it's best if this were closed, actually. At first I thought I was doing something wrong, but it appears to be more of a functionality with Nifty/JME. I'm slowly figuring out the limitations and working around it.. and if I need help the JME forums would probably be best. Thanks Ricket! – Snailer Nov 9 '11 at 23:58

Depending on how your implement the reference to the ScreenController Nifty will create a new instance of the referred class. I guess this is the problem that is pestering you. It results in two instances of this class, one that is maintained by JME and that contains the proper references to the player and so on, and one newly created that contains no references but is used and referred to by Nifty.
To solve this you have to use the registerScreenController() funktion auf the Nifty class to register the correct instance of the screen controller before the actual GUI is load from the XML file or build using Java.