ActionListener still triggers after Nifty GUI button has been clicked

I’m developing a trading card game that currently consists of two screens - the main menu and a game screen. The game screen contains a button that allows the player to forfeit that game and get back to the main menu. This button worked like a charm until I added an action listener to the class that handles the game functionality and that triggers when the user does a left mouse click. In that case, it does things like checking whether the user clicked on a card on his hand or on an empty square on the board to place the card right there.

When the user clicks on the forfeit button, the backToMainMenu() method of the corresponding screen class is called, which looks like this:

[java]
public void backToMainMenu() {

    stateManager.detach(stateManager.getState(QuickGameSetup.class));
    stateManager.detach(stateManager.getState(QuickGame.class));
    stateManager.detach(this);
    rootNode.detachAllChildren();
    guiNode.detachAllChildren();
    
    StartScreen startScreen = new StartScreen(game, niftyDisplay);
    stateManager.attach(startScreen);

}[/java]

So it essentially detaches everything that’s part of the game right now because all I need afterwards is the main menu. The problem now is that apparently the action listener still triggers after jME started the detachment process because I’m getting a NullPointerException in my action listener in this line:

[java]
if((guiNode.getChild("Card " + playerHand.get(i).getName()).getLocalTranslation().getX() <= position.x)[/java]

It doesn’t happen always, but most of the time. The game obviously tries to get that child of the guiNode, but since every single child has been detached already, it doesn’t find anything.

My question is - how can I avoid that behavior? How can I prevent jME from entering the action listener when I click on a Nifty GUI button - or at least how can I prevent it from entering it AFTER the children have been detached?

Solved this problem. I didn’t detach the actionListener in the cleanup-Method.