# Slick & NiftyGUI. Nifty initialize exception

I found my self into trouble when trying to run a Slick game with a Nifty Game State. This is the code:

@Override
protected void initGameAndGUI(GameContainer container, StateBasedGame game)
throws SlickException {
initNifty(container, game);
}


If i run this i get: java.lang.IllegalStateException: The NiftyGUI was already initialized. Its illegal to do so twice.

If i delete the call to initNifty() i get another exception:java.lang.IllegalStateException: NiftyGUI was not initialized.

• for some reason the game state is initialized twice during the startup. Its correct to call initNifty(). Could you please edit the question and add the code where you add the game state of the game (that must not happen more then once) and where your launch your game? Apr 6, 2012 at 20:59
• I got rid of nifty gui because several people told me TWL is a better choice so now i found myself trying to learn TWL. BTW great game. One of the reasons i have chosen java as THE LANGUAGE. Apr 6, 2012 at 21:03
• Okay. Can't help you with TWL how ever. I stayed with Nifty-GUI. Apr 6, 2012 at 22:09
• Anyway thanx for trying. Apr 6, 2012 at 22:20

This evening I switched switched from TWL to Nifty to give it a try. I experienced some of the same issues as you. I did get it working though. The documentation isn't great and there's no complete example at the end, so it's hard to know exactly what you're supposed to do at a glance. Below I've included the function I implemented to properly set up Nifty.

As the documentation states(see manual), your main game class is supposed to extend NiftyOverlayGame, or one of the other classes it lists, as I answered in your other question.

One of the functions you need to override is initGameAndGUI and I believe this is where your issue originates from. This is how I did it properly:

protected void initGameAndGUI(GameContainer gc) throws SlickException {
renderDevice = new SlickRenderDevice(gc);
soundDevice = new SlickSoundDevice();
inputSystem = new SlickSlickInputSystem(new PlainSlickInputSystem());
timeProvider = new LWJGLTimeProvider();
gui = new Nifty(renderDevice, soundDevice, inputSystem, timeProvider);
initNifty(gc);
}


Where these objects were defined inside my main class as (I'll include the imports as well for reference):

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.slick2d.NiftyOverlayGame;
import de.lessvoid.nifty.slick2d.input.PlainSlickInputSystem;
import de.lessvoid.nifty.slick2d.input.SlickSlickInputSystem;
import de.lessvoid.nifty.slick2d.render.SlickRenderDevice;
import de.lessvoid.nifty.slick2d.sound.SlickSoundDevice;
import de.lessvoid.nifty.slick2d.time.LWJGLTimeProvider;
import de.lessvoid.nifty.spi.input.InputSystem;
import de.lessvoid.nifty.spi.sound.SoundDevice;

...

public final class MainGame extends NiftyOverlayGame  {
Nifty gui;
SoundDevice soundDevice;
SlickRenderDevice renderDevice;
InputSystem inputSystem;
TimeProvider timeProvider;
...


Hopefully setting up your code similar to this should help you get on the right track (if you haven't figured this out already).

I'm using nifty 1.3.2 and I had the same issue. Whenever I use a state based implementation, I got the error described above. This seems definitely to be a bug in nifty. Their examples does not implement slicks state-based implementations...

I could make it running by manipulating the source and build my own jar file of nifty-slick-renderer-1.3.2

I know the following change does not solve the issue, but will make your code run. Change

if (guiPrepared) {
throw new IllegalStateException("The NiftyGUI was already initialized. Its illegal to do so twice.");
}


to

if (guiPrepared) {
return;
}


and rebuild the jar file (and name it custom or so). This change will just ignore the second run of trying to build nifty.