I'm using Slick2D and Nifty GUI. I decided to use a state based approach for my game and since I want to use Nifty GUI, I use the classes NiftyStateBasedGame for the main and NiftyOverlayBasicGameState for the states.

As the description say, I'm suppose to initialize the GUI in the method initGameAndGUI on my states, no problem:

protected void initGameAndGUI(GameContainer gc, StateBasedGame sbg) throws SlickException {
    initNifty(gc, sbg) 

It works great when I have only one state but if I'm doing a call to initNifty several times from different states, it will raise the following exception:

org.bushe.swing.event.EventServiceExistsException: An event service by the name NiftyEventBusalready exists.  Perhaps multiple threads tried to create a service about the same time?
at org.bushe.swing.event.EventServiceLocator.setEventService(EventServiceLocator.java:123)
at de.lessvoid.nifty.Nifty.initalizeEventBus(Nifty.java:221)
at de.lessvoid.nifty.Nifty.initialize(Nifty.java:201)
at de.lessvoid.nifty.Nifty.<init>(Nifty.java:142)
at de.lessvoid.nifty.slick2d.NiftyCarrier.initNifty(NiftyCarrier.java:94)
at de.lessvoid.nifty.slick2d.NiftyOverlayBasicGameState.initNifty(NiftyOverlayBasicGameState.java:332)
at de.lessvoid.nifty.slick2d.NiftyOverlayBasicGameState.initNifty(NiftyOverlayBasicGameState.java:299)
at de.lessvoid.nifty.slick2d.NiftyOverlayBasicGameState.initNifty(NiftyOverlayBasicGameState.java:280)
at de.lessvoid.nifty.slick2d.NiftyOverlayBasicGameState.initNifty(NiftyOverlayBasicGameState.java:264)

The initializeEventBus that raises the exception is called from the Nifty constructor and a new Nifty object is created within the initNifty method:

public void initNifty(
    final SlickRenderDevice renderDevice,
    final SlickSoundDevice soundDevice,
    final SlickInputSystem inputSystem,
    final TimeProvider timeProvider) {
    if (isInitialized()) {
       throw new IllegalStateException("The Nifty-GUI was already initialized. Its     illegal to do so twice.");

    final InputSystem activeInputSystem;
    if (relayInputSystem == null) {
       activeInputSystem = inputSystem;
    } else {
      activeInputSystem = relayInputSystem;

    nifty = new Nifty(renderDevice, soundDevice, activeInputSystem, timeProvider);

Is this a bug in the Nifty for Slick2D implementation or am I missing something? How am I supposed to handle Nifty initialization over multiple states?

  • \$\begingroup\$ "Perhaps multiple threads tried to create a service about the same time?" I'm not familiar with Slick, or even Java, but what about a global mutex, so initNifty can only be called once another has finished. Is that possible or am I way off? \$\endgroup\$ Commented Jul 12, 2013 at 1:26

1 Answer 1


I am not familiar with the Nifty GUI, so take this advice with a grain of salt, but I would suggest trying to determine if you really need to initialize the GUI multiple times. It might be better to simply initialize once, and use other methods to make the changes you need when you change states.

If this is the case, you can either store a boolean value to track whether you have initialized the GUI already, and check this before calling initNifty. If you have already initialized it, simply skip the initialization call.

  • \$\begingroup\$ I actually can't do that. Nifty will create a new instance for each new state so the nifty object i have in my state will never be initialized. The problem is that this new Nifty instance HAS TO be initialized, even if i don't use it in my state. So a call to initNifty is needed in each states, which lead to my problem since it will reinitialize the event bus with the same name (i looked the code) and then raisethe exception. I'm really starting to think that there is something wrong in the slick2d nifty implementation. \$\endgroup\$
    – nathan
    Commented Nov 13, 2012 at 19:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .