XYChart.Series.getData()。clear()抛出IndexOutOfBoundsException

时间:2015-05-22 11:41:54

标签: java javafx javafx-8

当我尝试清除我的收藏(链接到lineChart)时,我有一个IndexOutOfBoundsException。根据另一篇文章,我尝试使用Platform.runlater,但问题还在继续。我想知道是否有人有同样的问题以及如何解决它?

public void loadFullData(){
  System.out.println("processing");
  Platform.runLater(new Runnable() {
   @Override public void run() {
    data.getPeaksCol().getData().clear();
    data.getPeaksCol().getData().addAll(newData);
  }});
}

编辑: 完整的stackTrace:

processing
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index: 2036, Size: 2036
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at javafx.scene.chart.XYChart$Series.getItem(XYChart.java:1628)
    at javafx.scene.chart.LineChart.dataItemRemoved(LineChart.java:305)
    at javafx.scene.chart.XYChart.dataItemsChanged(XYChart.java:497)
    at javafx.scene.chart.XYChart.access$2300(XYChart.java:93)
    at javafx.scene.chart.XYChart$Series$1.onChanged(XYChart.java:1457)
    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 com.sun.javafx.collections.ObservableListWrapper.clear(ObservableListWrapper.java:157)
    at graphModel.GraphModel$1.run(GraphModel.java:127)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/1079803749.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
    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$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:2)

我能够使用oracle的线条图教程重现错误,只需添加一个按钮,点击该按钮即可删除唯一系列的所有数据。看起来像是由于图表动画导致的竞争条件错误。快速解决方法是在清除所有数据时禁用动画:

lineChart.setAnimated(false);
series.getData().clear();
lineChart.setAnimated(true);

我认为该错误代码为LineChart.dataItemRemoved(...)

if ( itemIndex > 0 && itemIndex < series.getDataSize() ) {
    animate = true;
    XYChart.Data<X, Y> p1 = series.getItem( itemIndex - 1 );
    XYChart.Data<X, Y> p2 = series.getItem( itemIndex + 1 );
    ...
    ...
}

如果itemIndex = series.getDataSize() - 1series.getItem( itemIndex + 1 )将投掷IndexOutOfBoundsException