JavaFX Chart - Java.lang.IllegalArgumentException:添加了重复的系列

时间:2016-07-14 08:49:31

标签: java exception javafx charts

我使用clear()方法从图表中删除数据。

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

    axesTest.getData().clear();
}

在这个功能中,但是一旦点击按钮 - 它就可以工作..但是第二次,当我要点击它时,它会抛出异常:

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)

所以问题出在138行以及159行代码中:

@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);
    }
}

}

问题在于:

axesTest.getData().addAll(seriesList);

为什么在清除图表中的所有数据后会出现此异常?我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:3)

您清除ObservableListSeries中存储的axesTest.getData().clear();

seriesList

但您使用名为Data的中间列表(?),而不是直接添加seriesList.add(series); seriesList.add(series1); seriesList.add(series2) ... axesTest.getData().addAll(seriesList);

seriesList

问题是你在添加元素之前没有清除gaussianWave = new GaussianWave( seriesList, axes, timeLine ); seriesList = gaussianWave.draw(); ,所以它包含第二次点击两次的元素,这将产生dataProperty

实际上我不知道这里发生了什么:

seriesList

但似乎Data包含这些行之后的先前{ "1": 0, "3": 0, "2": 0...个对象。