我添加一些元素到一個ObservableList
JavaFX
和添加一些項目後,我開始得到NullPointerException
我添加的所有項目。按照JavaDoc
的add()
方法拋出NullPointerException
時:ObservableList throwing NullPointerException
NullPointerException - 如果指定的元素爲null,並且此列表 不允許null元素
但正如你所看到的,而我調試我的元素不爲空:
那麼,爲什麼我收到此NullPointerException
?
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at javafx.scene.chart.XYChart$Series$1.onChanged(XYChart.java:1525)
at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:158)
at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:72)
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.add(ModifiableObservableListBase.java:155)
at java.util.AbstractList.add(AbstractList.java:108)
at sample.Main$1.handle(Main.java:115)
at javafx.animation.AnimationTimer$AnimationTimerReceiver$1.run(AnimationTimer.java:58)
at javafx.animation.AnimationTimer$AnimationTimerReceiver$1.run(AnimationTimer.java:56)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.animation.AnimationTimer$AnimationTimerReceiver.handle(AnimationTimer.java:56)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:359)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:269)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:475)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:460)
at com.sun.javafx.tk.quantum.QuantumToolkit$13.run(QuantumToolkit.java:327)
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.access$300(WinApplication.java:39)
at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112)
at java.lang.Thread.run(Thread.java:745)
哪裏sample.Main$1.handle(Main.java:115)
是我使用add()
方法。
編輯:
看來,當我裏面添加東西的物品,如TimerTask()
或AnimationTimer()
它只發生:
new AnimationTimer(){
@Override
public void handle(long now) {
for (XYChart.Data<Double, Double> data : fullDataQueue){
chartData.add(data);
}
}
}.start();
否則我沒有得到這個厚望!
編輯2:
好吧,我想我知道問題是什麼,這裏是測試代碼:
@Test
public void testSeriesAddInAnimator(){
// Data
XYChart.Series<Double, Double> series = new XYChart.Series<>();
ConcurrentLinkedQueue<XYChart.Data<Double, Double>> fullDataQueue = new ConcurrentLinkedQueue<>();
ObservableList<XYChart.Data<Double, Double>> chartData = FXCollections.observableArrayList();
series.setData(chartData);
// Start adding data
final Random random = new Random();
for(int n = 0; n < 10000; ++n){
fullDataQueue.add(new XYChart.Data<>((double)n, random.nextDouble()));
}
new AnimationTimer(){
@Override
public void handle(long now) {
for (XYChart.Data<Double, Double> data : fullDataQueue){
chartData.add(data);
}
System.out.println("Stop!");
//stop(); -> Uncomment this and we get no exception!
}
}.start();
System.out.println("Done testSeriesAddInAnimator()!");
}
這似乎發生在系列,如果我再補充現有數據。這不是RT-37798錯誤,但我認爲這也是一個錯誤。如果我們在AnimationTimer
內對stop()
方法發表評論,則將重新添加相同的數據,並且我們開始獲取所有這些NullPointerException
。
唯一的例外是在XYChart.java
線1525:
for(int i=c.getFrom(); i<c.getTo(); i++) {
getData().get(i).setSeries(Series.this);
// update linkedList Pointers for data in this series
if (begin == null) {
begin = getData().get(i);
begin.next = null;
} else {
if (i == 0) {
getData().get(0).next = begin;
begin = getData().get(0);
} else {
Data<X,Y> ptr = begin;
for (int j = 0; j < i -1 ; j++) {
ptr = ptr.next; // NPE HERE!!!!!
}
getData().get(i).next = ptr.next;
ptr.next = getData().get(i);
}
}
}
但是,如果我嘗試刪除所有項目在ObservableList
然後添加我想我得到有關你提到的錯誤的NullPointerException
的那些(RT- 37798)。所以看起來我註定了,在修正這個錯誤之前我沒有辦法做到。
看看這個問題和答案:http://stackoverflow.com/a/24359013/682840 –
所以,又一個JavaFX錯誤,我正在考慮再次切換到Swing ... – Andres
@ JohnM.Wright看到我的編輯問題 – Andres