ConcurrentModification Exception when adding items to a list

Hey guys.



I’m trying to implement a game browser with nifty but I’m receiving ConcurrentModification exceptions when I’m adding items to the list.

The following is the code where I add game-names to a list.

[java]private void updateGameList() {



ListBoxControl lobbygameList = screen.findControl(“listBoxStatic”, ListBoxControl.class);

//receives a List

List<GamesListGameMessage> gameList=StaticData.getCustomGamesList().getGames();



if (gameList.size() > 0) {

for (GamesListGameMessage e : gameList) {

lobbygameList.addItem(" " + e.getGameId() + "t t "

  • e.getHostNick() + "t t" + e.getUserCount() + "/"
  • e.getUserMax()); //

    }

    } else {

    lobbygameList.addItem("No games available");

    }

    }[/java]



    and this is the xml part:

    [xml]

    <panel id="listBoxPanel" childLayout="vertical" width="100%" height="70%" padding="30px,30px,30px,30px">

    <control id="listBoxStatic" name="listBox">

    <panel id="listBoxStaticData" width="100%" childLayout="vertical" visibleToMouse="true" />

    </control>

    </panel>

    [/xml]



    Things I already tried out:
  • when I start adding simple integer values to the list (1…10) I receive the exception after the second integer.
  • the method doesnt get called twice



    Any suggestions would be greatly appreciated.



    Kaizo



    EDIT: I should add that updateGameList() gets called when I click on a refresh button at the bottom of the screen.

Only thing that comes to my mind is that the lobbygameList is probably being iterated else where in the mean time.

Maybe Nifty iterate over it to refresh it?

The button is a nifty button or an AWT button? In the latter case you have a threading problem.

It is a nifty button. I will try to create a testcase.



One more thing: Sometimes it does work when I’m only adding one item to the list and I’m not pressing the button in rapid succession. Only sometimes though :frowning:

Small update:

The testcase worked without any problems. It probably IS a threading problem since I forgot to mention that the method doesn’t get called directly by the button. The button sends a request for a game list which THEN sends a notice to nifty when it has been received. I would’ve never imagined that this small detour creates such a big problem.



Now I need a new desk. In the last few days the bitemarks have taken over the majority of it… arrrgh



EDIT:

Alright I think I’ve solved it.

I used the following code:

[java]

nifty.delayedMethodInvoke(new NiftyDelayedMethodInvoke(){

@Override

public void performInvoke(Object… arg0) {

updateGameList();

}

}, null);[/java]



I hope this doesn’t cause problems in the long run.