1

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:

enter image description here

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.

Andres
  • 6,080
  • 13
  • 60
  • 110
  • 1
    Take a look at this question and answer: http://stackoverflow.com/a/24359013/682840 – John M. Wright Jul 06 '14 at 04:58
  • So, again another JavaFX bug, I am considering switching to Swing again... – Andres Jul 06 '14 at 05:05
  • could be yet another variant of a bug I recently reported: https://javafx-jira.kenai.com/browse/RT-37798 – kleopatra Jul 06 '14 at 08:52
  • can you show a small runnable example for me to test my assumption? – kleopatra Jul 06 '14 at 08:58
  • looking at the example (for the first time, sorry) - seems like that's different from your real-world context in that it adds the same instance of a data item more than once which isn't allowed (and doesn't make much sense :). Just saying ... – kleopatra Jul 09 '14 at 08:12

2 Answers2

1

It looks like it isn't data, but one of datas components that is null.

I couldn't find matching source code, but if you look at

javafx.scene.chart.XYChart$Series$1.onChanged(XYChart.java:1525)

Im confident that you find a dereferencing of a component of data.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • if it's really related to https://javafx-jira.kenai.com/browse/RT-37798 - as I suspect - there is nothing to do except waiting for the bug to be fixed :-( The deeper reason is a highly coupled design/implementation with back and forth callbacks all over the place and no clear (read: not documented) contract as to who is responsible for what when. – kleopatra Jul 06 '14 at 08:55
0

Ok it seems that RT-37798 bug is solved in JDK 8u20 Build 21. Will submit the bug I am having to see If I can get some response (https://javafx-jira.kenai.com/browse/RT-37824).

Thank you for your help!

Andres
  • 6,080
  • 13
  • 60
  • 110
  • 1
    careful: the fix in u20 could be just cosmetics - no longer throwing an NPE but the series' internal data structure might be out of sync with the list of data items – kleopatra Jul 07 '14 at 12:44
  • Yes I have read your comment so I decided to deploy my own chart library. Thank you your help! – Andres Jul 07 '14 at 14:19