yerpy
yerpy

Reputation: 1446

JavaFX Chart - Java.lang.IllegalArgumentException : Duplicate series added

I'm using clear() method for removing data from chart.

@FXML
public void restartButtonClicked() {
    System.out.println("Restarting...");
    timeLine.getKeyFrames().clear();

    axesTest.getData().clear();
}

In this function, but once button get clicked - it works.. but second time while I am gonna click it, it throw exception :

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Duplicate series added
    at javafx.scene.chart.XYChart.lambda$new$550(Unknown Source)
    at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(Unknown Source)
    at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(Unknown Source)
    at javafx.collections.ObservableListBase.fireChange(Unknown Source)
    at javafx.collections.ListChangeBuilder.commit(Unknown Source)
    at javafx.collections.ListChangeBuilder.endChange(Unknown Source)
    at javafx.collections.ObservableListBase.endChange(Unknown Source)
    at javafx.collections.ModifiableObservableListBase.addAll(Unknown Source)
    at sample.Controller.startButtonClicked(Controller.java:138)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.Trampoline.invoke(Unknown Source)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Node.fireEvent(Unknown Source)
    at javafx.scene.control.Button.fire(Unknown Source)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
    at javafx.event.Event.fireEvent(Unknown Source)
    at javafx.scene.Scene$MouseHandler.process(Unknown Source)
    at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(Unknown Source)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
    at com.sun.glass.ui.View.notifyMouse(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

So the problem is in 138 line as well as in 159 line of code from method :

@SuppressWarnings("unchecked")
@FXML
public void startButtonClicked(ActionEvent event) {
if (comboBox.getValue() == null) {
    System.out.print("Nie wybrano opcji.");
} else {
    if(comboBox.getValue().equals("Gaussian Wave : non-Dispersive")) {
        System.out.println("Gaussian Wave : non-Dispersive");
        axesTest.setTitle("Gaussian Wave : non-Dispersive");

        series.setName("bla");
        series1.setName("ssad");
        series2.setName("asf");

        seriesList.add(series);
        seriesList.add(series1);
        seriesList.add(series2);

        timeLine = new Timeline();
        gaussianWave = new GaussianWave( seriesList, axes, timeLine );
        seriesList = gaussianWave.draw();
        gaussianWave.update(axesTest);

        axesTest.getData().retainAll();
        axesTest.getData().addAll(seriesList);
    } else if(comboBox.getValue().equals("Gaussian Wave : Dispersive")) {
        System.out.println("Gaussian Wave : Dispersive");
        axesTest.setTitle("Gaussian Wave : Dispersive");

        series.setName("bla");
        series1.setName("ssad");
        series2.setName("asf");

        seriesList.add(series);
        seriesList.add(series1);
        seriesList.add(series2);

        timeLine = new Timeline();
        gaussianWaveDispersive = new GaussianWaveDispersive( seriesList, axes, timeLine );
        seriesList = gaussianWaveDispersive.draw();
        gaussianWaveDispersive.update(axesTest);

        axesTest.getData().retainAll();
        axesTest.getData().addAll(seriesList);
    }
}

}

and the problem is in :

axesTest.getData().addAll(seriesList);

Why do I get this exception after clearing all of data from the chart ? How could I fix this?

Upvotes: 2

Views: 3820

Answers (1)

DVarga
DVarga

Reputation: 21799

You clear the ObservableList stored in the dataProperty of the Series:

axesTest.getData().clear();

but you use an intermediate list(?) called seriesList, rather than adding Data directly:

seriesList.add(series);
seriesList.add(series1);
seriesList.add(series2)

...

axesTest.getData().addAll(seriesList);

The problem is you don't clear seriesList before adding the elements, so it contains the elements on the second click twice, which will result in an IllegalArgumentException.

Actually I don't know what happens here:

gaussianWave = new GaussianWave( seriesList, axes, timeLine );
seriesList = gaussianWave.draw();

but it seems that seriesList contains the previous Data objects after these lines.

Upvotes: 3

Related Questions