Null pointer exception on nifty popup

Hi,



I’m trying to use the popuplayer, but I get a Null pointer exception (and the nifty is already initialized and with two fully functional screens) the code of the popup is like this (as shown on http://sourceforge.net/apps/mediawiki/nifty-gui/index.php?title=Popup_Layer_Reference):



new PopupBuilder(“popupExit”) {

{

childLayoutCenter();

backgroundColor("#000a");

}

}.registerPopup(nifty);

exitPopup = nifty.createPopup(“popupExit”); //here, createPopup throws a NullPointerException (line 462)



And this is the stack trace:



java.lang.NullPointerException

at de.lessvoid.nifty.Nifty.createPopupFromType(Nifty.java:807)

at de.lessvoid.nifty.Nifty.createAndAddPopup(Nifty.java:859)

at de.lessvoid.nifty.Nifty.createPopup(Nifty.java:834)

at satira.cliente.Cliente.initBaseScreens(MyClient.java:462)



What I’m doing wrong?

Well, your code is fine. Your nifty object is null.

Thanks for the reply, but no it isn’t, this is the code imediately before the code i posted (plus, if the nifty object were null, the NullPointerException would have been thrown on my method initBaseScreens not in createPopupFromType):



NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(

assetManager,

inputManager,

audioRenderer,

guiViewPort);

nifty = niftyDisplay.getNifty();

guiViewPort.addProcessor(niftyDisplay);

nifty.loadStyleFile(“nifty-default-styles.xml”);

nifty.loadControlFile(“nifty-default-controls.xml”);

nifty.addScreen(“start”, new ScreenBuilder(“start”) {



{

controller(new SeleccionarHostScreen());

layer(new LayerBuilder(“background”) {



{

childLayoutCenter();

}

});

layer(new LayerBuilder(“foreground”) {



{

childLayoutVertical();

panel(new PanelBuilder(“panel_top”) {



{

childLayoutVertical();

alignCenter();

height(“25%”);

width(“75%”);

text(new TextBuilder() {



{

text(“Satira MMORPG”);

font(“Interface/Fonts/Default.fnt”);

height(“50%”);

width(“100%”);

}

});

text(new TextBuilder() {



{

ResourceBundle mensaje = Cliente.getInstance().getMensajes(“bienvenida”);

text(mensaje.getString(“seleccionahost”));

font(“Interface/Fonts/Default.fnt”);

height(“50%”);

width(“100%”);

}

});

}

});

panel(new PanelBuilder(“panel_mid”) {



{

childLayoutVertical();

alignCenter();

height(“50%”);

width(“75%”);

}

});

panel(new PanelBuilder(“panel_bottom”) {



{

childLayoutHorizontal();

alignLeft();

height(“25%”);

width(“75%”);

}

});

}

});

}

}.build(nifty));

Well, the code is fine. Try updating the with nightly builds.

The nightly build didn’t work… anyway, I guess the popup can be replaced with layers and some traditional logic.



Thanks for your help.

Layers will make the maintenace hard instead. Maybe @ractoc know what is going on.

What exactly is it you are trying to do with the popup? If you are trying to create a MessageBox, there’s a standard control for that now. If not, you could look at the code / xml of the nifty-messagebox control and see how that one works. Should give you a basic idea how to work with popups.

Thanks, I’ll give it a try and tell you later if it worked.

I’ve run into the exact same problem. I add the popup like so.



[java]public void startScreen(){

NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(

assetManager, inputManager, audioRenderer, guiViewPort);

Nifty nifty = niftyDisplay.getNifty();

nifty.fromXml(“Interface/screen.xml”, “start”, new TitleScreen());

guiViewPort.addProcessor(niftyDisplay);

flyCam.setEnabled(false);





/** Add Popups and populate array with them*/

Element popTitle = nifty.createPopup(“popupTitle”); // <


See added it
popArray.add(popTitle);
}[/java]

Just like cedadcamus I get a null pointer exception on that line. So I went to see if I had that popup id right and I do.

[xml]<popup id="popupTitle" childLayout="center" backgroundColor="#000a">
<panel id="popupTitle" height="10%" width="15%" align="center" childLayout="center"
backgroundImage="Interface/sizebox.png" imageMode="resize:15,2,15,15,15,2,15,2,15,2,15,15">
<control name="button" label="Back to Title" id="toTitle" align="center" valign="center"
visibleToMouse="true" >
<interact onClick="changeScreen(start)" />
</control>
</panel>
</popup>
[/xml]

I updated with nightly builds like suggested. I'm trying to create a "Sure you want to exit?" type of popup with a yes or no button. I can't seem to figure out why I would be getting that error. Any insight would help alot.

You have the same id for both the popup and panel, perhaps that could create problems?

I’m using modified versions of the MessageBox control for my popups and that works well (though I’m using createPopupWithId(id, id)).

Also, perhaps you could try creating your popups in the screen controller for the screen you’re loading (in the bind method), so you’ll know for sure that the screen is bound?

I tired changing the names, and that didn’t help though it’s probably good to have them changed. I also tried creating the popup in the screen controller, it still gives me the same null pointer exception. So for some reason it’s not finding it in my screen.xml I guess? I know how to create new controllers and styles for nifty but I can’t seem to find on the messagebox control (In the source nifty provided on source forge) to base the custom message box control off of.

By the way, where in your xml file is the popup declaration?

It took me some time at first to figure out that it needs to be outside of any screen tag.

The MessageBox is in nifty-controls/src/main/java/de/lessvoid/nifty/controls: http://nifty-gui.git.sourceforge.net/git/gitweb.cgi?p=nifty-gui/nifty;a=blob;f=nifty-controls/src/main/java/de/lessvoid/nifty/controls/MessageBox.java

Oh! That’s why. I didn’t know that, thank you. Thanks for the link too, I’ll be sure to look that over.