title: onError java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
body: <p>I have written the following code for finding <em>pageCount</em> of comics that lie within certain <em>budget</em>.</p>
  
  <p>At first I tried to come up with code that would have architecture like this:</p>
  
  <ul>
  <li>Stream gives price of a <em>MarvelComic</em> object.</li>
  <li>I sum the price of the <em>MarvelComic</em> object from stream with prices of the previous comics that came down this stream and check if it's &lt; <strong>BUDGET</strong></li>
  <li>If Yes, then I sum the <em>pageCount</em> of the <em>MarvelComic</em> object with the pageCount sum of previous <em>MarvelComic</em> objects that came down the stream.</li>
  <li>If yes, then <em>onNext</em> of the subscriber is called.</li>
  </ul>
  
  <p>Since I couldn't devise a way to write code like I mentioned in the steps above, I resorted to mashing imperative programming with Reactive Programming. As a result I wrote the following code:</p>
  
  <pre><code>Observable.fromIterable(getMarvelComicsList()).
                  map(new Function&lt;MarvelComic, HashMap&lt;String, Double&gt;&gt;() {
                      @Override
                      public HashMap&lt;String, Double&gt; apply(@NonNull MarvelComic marvelComic) throws Exception {
                          HashMap&lt;String, Double&gt; map = new HashMap&lt;String, Double&gt;();
                          map.put("price", Double.valueOf(marvelComic.getPrice()));
                          map.put("pageCount", Double.valueOf(marvelComic.getPageCount()));
                          map.put("comicCount", Double.valueOf(marvelComic.getPageCount()));
                          return map;
                      }
                  })
                  .scan(new HashMap&lt;String, Double&gt;(), new BiFunction&lt;HashMap&lt;String, Double&gt;, HashMap&lt;String, Double&gt;, HashMap&lt;String, Double&gt;&gt;() {
                      @Override
                      public HashMap&lt;String, Double&gt; apply(@NonNull HashMap&lt;String, Double&gt; inputMap, @NonNull HashMap&lt;String, Double&gt; newValueMap) throws Exception {
                          double sum = inputMap.get("price")+newValueMap.get("price");
                          double count = inputMap.get("pageCount")+newValueMap.get("pageCount");
                          double comicCount = inputMap.get("comicCount")+newValueMap.get("comicCount");
  
                          HashMap&lt;String, Double&gt; map = new HashMap&lt;String, Double&gt;();
                          map.put("price", sum);
                          map.put("pageCount", count);
                          map.put("comicCount", comicCount);
  
                          return map;
                      }
                  })
                  .takeWhile(new Predicate&lt;HashMap&lt;String, Double&gt;&gt;() {
                      @Override
                      public boolean test(@NonNull HashMap&lt;String, Double&gt; stringDoubleHashMap) throws Exception {
                          return stringDoubleHashMap.get("price") &lt; budget;
                      }
                  })
                  .subscribe(new DisposableObserver&lt;HashMap&lt;String, Double&gt;&gt;() {
                      @Override
                      public void onNext(HashMap&lt;String, Double&gt; stringDoubleHashMap) {
                          double sum = stringDoubleHashMap.get("price");
                          double pageCount = stringDoubleHashMap.get("pageCount");
                          double comicCount = stringDoubleHashMap.get("comicCount");
                          Timber.e("sum %s  pageCount %s  ComicCount: %s", sum, pageCount, comicCount);
                      }
  
                      @Override
                      public void onError(Throwable e) {
                          Timber.e("onError %s", e.fillInStackTrace());
                      }
  
                      @Override
                      public void onComplete() {
                          Timber.e("onComplete");
                      }
                  });
  </code></pre>
  
  <p><strong>My Reservations:</strong></p>
  
  <ol>
  <li>Is it good idea to create a new Hashmap every time inside <code>map(), scan()</code>?</li>
  <li>How can I improve this code further?</li>
  </ol>
  
  <p><strong>Issues:</strong></p>
  
  <p>This code gives <em>NullPointerException</em> in <code>onError</code> because <code>map.get("price")</code> returns null in <code>scan()</code>. I'm not really sure of the reason.</p>
  
  <p><strong>Error:</strong></p>
  
  <pre><code> onError java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
  </code></pre>
  
  <p><strong>NOTE:</strong></p>
  
  <p>HashMap isn't null, the double field is being returned as NULL for some reason. I'm trying to figure out how.</p>