title: ObservableList throwing NullPointerException
body: <p>I am adding some elements to an <code>ObservableList</code> in <code>JavaFX</code> and after adding some items I start getting <code>NullPointerException</code> for all the items I add. According to the <code>JavaDoc</code> the <code>add()</code> method throws <code>NullPointerException</code> when:</p>
  
  <blockquote>
    <p>NullPointerException - if the specified element is null and this list
    does not permit null elements</p>
  </blockquote>
  
  <p>But as you can see while I'm debugging my element is NOT null:</p>
  
  <p><img src="https://i.stack.imgur.com/w8l4v.jpg" alt="enter image description here"></p>
  
  <p>So why I am getting this <code>NullPointerException</code>?</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Where <code>sample.Main$1.handle(Main.java:115)</code> is where I use the <code>add()</code> method.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>It seems it only happens when I add the items inside things like <code>TimerTask()</code> or <code>AnimationTimer()</code>:</p>
  
  <pre><code>new AnimationTimer(){
              @Override
              public void handle(long now) {
                  for (XYChart.Data&lt;Double, Double&gt; data : fullDataQueue){
                      chartData.add(data);
                  }
              }
          }.start();
  </code></pre>
  
  <p>Otherwise I'm not getting this expection!</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Ok I think I know what the problem is, here is the test code:</p>
  
  <pre><code>@Test
      public void testSeriesAddInAnimator(){
          // Data
          XYChart.Series&lt;Double, Double&gt; series = new XYChart.Series&lt;&gt;();
          ConcurrentLinkedQueue&lt;XYChart.Data&lt;Double, Double&gt;&gt; fullDataQueue = new ConcurrentLinkedQueue&lt;&gt;();
          ObservableList&lt;XYChart.Data&lt;Double, Double&gt;&gt; chartData = FXCollections.observableArrayList();
  
          series.setData(chartData);
  
          // Start adding data
          final Random random = new Random();
          for(int n = 0; n &lt; 10000; ++n){
              fullDataQueue.add(new XYChart.Data&lt;&gt;((double)n, random.nextDouble()));
          }
  
          new AnimationTimer(){
              @Override
              public void handle(long now) {
                  for (XYChart.Data&lt;Double, Double&gt; data : fullDataQueue){
                      chartData.add(data);
                  }
                  System.out.println("Stop!");
                  //stop(); -&gt; Uncomment this and we get no exception!
              }
          }.start();
          System.out.println("Done testSeriesAddInAnimator()!");
      }
  </code></pre>
  
  <p>This seems to happens if I re-add existing data in the Series. It's not <a href="https://javafx-jira.kenai.com/browse/RT-37798" rel="nofollow noreferrer" title="RT-37798">RT-37798</a> bug, but I think it's a bug too. If we comment <code>stop()</code> method inside <code>AnimationTimer</code> the same data will be re-added and we start getting all this <code>NullPointerException</code>.</p>
  
  <p>The exception is in <code>XYChart.java</code> line 1525:</p>
  
  <pre><code>for(int i=c.getFrom(); i&lt;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&lt;X,Y&gt; ptr = begin;
                                  for (int j = 0; j &lt; i -1 ; j++) {
                                      ptr = ptr.next;  // NPE HERE!!!!!
                                  }
                                  getData().get(i).next = ptr.next;
                                  ptr.next = getData().get(i);
                              }
                          }
                      }
  </code></pre>
  
  <p>But if I try to remove all items in the <code>ObservableList</code> and then add the ones I want I get the <code>NullPointerException</code> related to the bug you mentioned (RT-37798). So it seems I'm doomed, nothing I can do until they fix the bug.</p>