Search code examples
javajavafxobservablelistunsupportedoperation

Modifing ObservableList through its changeListener throws UnsupportedOperationException


I was making a simple tic-tac-toe GUI-based game. To do this I used a 2D ObservableList, filled with Figures enumerator. Every move I change the value of the chosen cell to X or O and then I check if someone won or if the game ended with a draw. If so, I refill the list with blanks. At least that's how it is supposed t work. I faced the problem described in the title. This problem doesn't happen in any case, I don't understand what happens here and failed to find this on Google. Here is a simplified version of my code: (help appreciated)

public class Example{
    enum Figures { NONE, X, O }
    static ObservableList<ObservableList<Figures>> list = FXCollections.observableArrayList();

    public static void main(String... args) {
        for (int i = 0; i < 3; i++) {
            ObservableList<Figures> subList = FXCollections.observableArrayList();
            list.add(subList);

            subList.addListener((ListChangeListener<Figures>) change -> {
                while (change.next()) {
                    Figures figure = subList.get(change.getFrom());
                    if (figure == Figures.NONE) {
                        //show Blank space
                    } else {
                        if (figure == Figures.X) {
                            //show X
                        }
                        if (figure == Figures.O) {
                            //show O
                        }

                        checkState();
                    }
                }
            });
            for (int j = 0; j < 3; j++) {
                subList.add(Figures.NONE);
            }
        }

        //it is a graphical game but here is what I do by clicking buttons to get the exception
        list.get(2).set(0, Figures.X);
        list.get(1).set(0, Figures.O);
        list.get(1).set(1, Figures.X);
        list.get(0).set(1, Figures.O);
        list.get(0).set(2, Figures.X);
    }

    private static void checkState() {
        //checking state and if win or draw
            restartGame();
    }

    private static void restartGame() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                list.get(i).set(j, Figures.NONE);
            }
        }
    }
}

And the output

Exception in thread "JavaFX Application Thread" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableList.add(Collections.java:1314)
at javafx.collections.ListChangeBuilder.nextRemove(ListChangeBuilder.java:208)
at javafx.collections.ListChangeBuilder.nextSet(ListChangeBuilder.java:453)
at javafx.collections.ObservableListBase.nextSet(ObservableListBase.java:115)
at javafx.collections.ModifiableObservableListBase.set(ModifiableObservableListBase.java:162)
at Main.restartGame(Main.java:239)
at Main.lambda$gameScene$2(Main.java:79)
at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:164)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
at javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
at javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
at javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
at javafx.collections.ModifiableObservableListBase.set(ModifiableObservableListBase.java:163)
at Main.gameScene(Main.java:170)
at Main.start(Main.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Exception in thread "JavaFX Application Thread" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableList.add(Collections.java:1314)
at javafx.collections.ListChangeBuilder.nextRemove(ListChangeBuilder.java:208)
at javafx.collections.ListChangeBuilder.nextSet(ListChangeBuilder.java:453)
at javafx.collections.ObservableListBase.nextSet(ObservableListBase.java:115)
at javafx.collections.ModifiableObservableListBase.set(ModifiableObservableListBase.java:162)
at Main.restartGame(Main.java:239)
at Main.lambda$gameScene$2(Main.java:79)
at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:164)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
at javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
at javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
at javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
at javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
at javafx.collections.ModifiableObservableListBase.set(ModifiableObservableListBase.java:163)
at Main.gameScene(Main.java:171)
at Main.start(Main.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

Solution

  • The problem is the checkState() function in your ListChangeListener. As per the javadoc at ListChangeListener

    Warning: This class directly accesses the source list to acquire information about the > changes. This effectively makes the Change object invalid when another change occurs on the list. For this reason it is not safe to use this class on a different thread. It also means the source list cannot be modified inside the listener since that would invalidate this Change object for all subsequent listeners.