So, I implemented today GUI interaction to be able to close and start the game. However, whenever the methods are called, my nifty object is null for some reason.
The Nifty object I get from the bind() method is the same as my nifty object
Here is my AppState that implements the ScreenController:
public void initialize() {
[...]
// Starts and loads the GUI
this.niftyJME = new NiftyJmeDisplay(assetManager, inputManager, audioRenderer, guiViewPort);
this.nifty = this.niftyJME.getNifty();
this.nifty.fromXml("Interface/gui.xml", "start", this);
this.nifty.registerScreenController(this);
this.app.getGuiViewPort().addProcessor(niftyJME);
// Continues to initialize
super.initialize(stateManager, app);
}
@Override
public void update(float tpf) {
}
@Override
public void bind(Nifty nifty, Screen screen) {
}
@Override
public void onStartScreen() {
}
@Override
public void onEndScreen() {
}
// Nifty's methods
public void startGame() {
nifty.removeScreen("start");
}
public void endGame() {
cleanup();
}
I had a problem like this, it was due to calling registerScreenController after calling fromXML. Try calling the registerScreenController before calling fromXML.
The “startGame” and “endGame” methods are the ones he’s calling in this example. In this case, I would try what neumann347 said. You are creating a new copy of the screen controller and you aren’t capturing the “nifty” object in the bind method so it is null.
@neumann347 and @glh3586 I tried registereing the screen before loading the XML but I still get the same problem. I also tried to catch the bind() Nifty object and pass it to my object. Still fails.
@pspeed sorry for not being clear. The two methods I am calling are, as glh3586 said, startGame() and endGame().
Well, I don’t see where ScreenController is mentioned in your XML… only something called MenuController… and I guess we don’t know what that code is. Either way, I guess it has not been registered with nifty and so nifty will create an instance for you using the no-arg constructor.
Anyway, I will let others help you as I swore I’d stop trying to help folks with nifty magic 5 years ago when I dropped it. Good luck, though… this is well trod territory, this issue.
Some users have been known to call initialize() manually. It’s very difficult for folks to assume things about code that they cannot see… which is why test cases are usually requested.
Okay… so to start with… I don’t think you tried to run this because pasting this all into a new project resulted in it not even running. The screen controller you reference in your gui.xml doesn’t reference the screen controller app state you posted. You referenced a completely different package structure. Look at the debug output as well. Once I fixed the gui.xml package name to match “appstates.MenuAppState” which is what you posted it ran fine and I didn’t get a null pointer error when I clicked start game. Your package structure looks a little weird for a java project.
Yeah, it was kinda my bad. As I said above, I was making a test, and when I pasted the code in the thread, I forgot to change the ScreenController reference.
So, I decided to to a test project like you did. I copied the AppState, the XML and the fonts. I created a simple app that literally only has the methods to attach the appState. I still crash with nulls.
You should probably show the code. Always always always shot the code. And when in doubt, show the code.
No one in my entire time of being on this forum has ever complained about having too much information in a post… but DAILY we complain about not enough.
Well removing it doesn’t just make it disappear but removes it from nifty. It’s possible you are causing a timing bug. Without seeing your stack trace and code for your test project I couldn’t say more.
Edit:
I just ran it again and got a null point error but not in the section you specified. Removing the screen was causing a null pointer error in the binding between jme and nifty.