title: Why is ListChangeBuilder throwing a NullPointerException?
body: <p>I'm trying to figure out why I'm getting this error:</p>
  
  <pre><code>java.lang.NullPointerException: null
      at javafx.collections.ListChangeBuilder.findSubChange(ListChangeBuilder.java:68)
      at javafx.collections.ListChangeBuilder.insertAdd(ListChangeBuilder.java:127)
      at javafx.collections.ListChangeBuilder.nextAdd(ListChangeBuilder.java:254)
      at javafx.collections.ObservableListBase.nextAdd(ObservableListBase.java:179)
      at javafx.collections.ModifiableObservableListBase.add(ModifiableObservableListBase.java:153)
      at java.util.AbstractList.add(AbstractList.java:108)
      at tech.dashman.dashman.RendererApp$1.handle(RendererApp.java:72)
      at tech.dashman.common.pubnub.NewScreenshotsToRender.handle(NewScreenshotsToRender.java:17)
      at tech.dashman.dashman.PubNubMessageHandler.lambda$message$0(PubNubMessageHandler.java:79)
      at java.lang.Thread.run(Thread.java:748)
  </code></pre>
  
  <p>It happened twice on my application but I don't know how to reproduce it. The line in my code that triggers the error is this:</p>
  
  <pre><code>getActivity().add(new ActivityEntry("..."));
  </code></pre>
  
  <p><code>ActivityEntry</code> is a simple class with two fields. I can post the code if necessary but I think it's irrelevant. <code>activity</code>, what <code>getActivity()</code> returns, is a field defined like this:</p>
  
  <pre><code>private ObservableList&lt;ActivityEntry&gt; activity = FXCollections.observableArrayList();
  </code></pre>
  
  <p>The method in which the actual exception is happening looks like this:</p>
  
  <pre><code>private int findSubChange(int idx, final List&lt;ListChangeBuilder.SubChange&lt;E&gt;&gt; list) {
      int from = 0;
      int to = list.size() - 1;
  
      while (from &lt;= to) {
          int changeIdx  = (from + to) / 2;
          ListChangeBuilder.SubChange&lt;E&gt; change = list.get(changeIdx);
  
          if (idx &gt;= change.to) {
              from = changeIdx + 1;
          } else if (idx &lt; change.from) {
              to = changeIdx - 1;
          } else {
              return changeIdx;
          }
      }
      return ~from;
  }
  </code></pre>
  
  <p>Line 68, the one raising the exception is:</p>
  
  <pre><code>if (idx &gt;= change.to) {
  </code></pre>
  
  <p>The only thing I can think of, other than a bug in <code>ListChangeBuilder</code>, is that it was a race condition and two threads modified the <code>ObservableList</code> at the same time. Does this look like that? Is <code>ObservableList</code> not thread safe?</p>