I am adding some elements to an ObservableList
in JavaFX
and after adding some items I start getting NullPointerException
for all the items I add. According to the JavaDoc
the add()
method throws NullPointerException
when:
NullPointerException - if the specified element is null and this list does not permit null elements
But as you can see while I'm debugging my element is NOT null:
So why I am getting this 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)
Where sample.Main$1.handle(Main.java:115)
is where I use the add()
method.
EDIT:
It seems it only happens when I add the items inside things like TimerTask()
or AnimationTimer()
:
new AnimationTimer(){
@Override
public void handle(long now) {
for (XYChart.Data<Double, Double> data : fullDataQueue){
chartData.add(data);
}
}
}.start();
Otherwise I'm not getting this expection!
EDIT 2:
Ok I think I know what the problem is, here is the test code:
@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()!");
}
This seems to happens if I re-add existing data in the Series. It's not RT-37798 bug, but I think it's a bug too. If we comment stop()
method inside AnimationTimer
the same data will be re-added and we start getting all this NullPointerException
.
The exception is in XYChart.java
line 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);
}
}
}
But if I try to remove all items in the ObservableList
and then add the ones I want I get the NullPointerException
related to the bug you mentioned (RT-37798). So it seems I'm doomed, nothing I can do until they fix the bug.