title: Handling null in RxJava2
body: <p>With the upcoming <a href="https://github.com/ReactiveX/RxJava/tree/2.x" rel="noreferrer">RxJava2 release</a> one of the important changes is that <code>null</code> is no longer accepted as a stream element, i.e. following code will throw an exception: <code>Observable.just(null)</code></p>
  
  <p>Honestly, I have mixed feelings about this change and part of me understands that it will enforce clean APIs, but I can see a number of use cases when this might be a problem.</p>
  
  <p>For instance, in my app I have an in-memory cache:</p>
  
  <pre><code>@Nullable CacheItem findCacheItem(long id);
  </code></pre>
  
  <p>CacheItem might not be present in cache, so method might return null value.</p>
  
  <p>The way it is used with Rx* - is as following:</p>
  
  <pre><code>Observable&lt;CacheItem&gt; getStream(final long id) {
      return Observable.fromCallable(new Callable&lt;CacheItem&gt;() {
          @Override public CacheItem call() throws Exception {
              return findCacheItem(id);
          }
      });
  }
  </code></pre>
  
  <p>So with this approach, I might get null in my stream which is totally valid situation, so it is handled properly on receiving side - let's say UI changes its state if item is not present in cache:</p>
  
  <pre><code>Observable.just(user)
            .map(user -&gt; user.getName())
            .map(name -&gt; convertNameToId(name))
            .flatMap(id -&gt; getStream(id))
            .map(cacheItem -&gt; getUserInfoFromCacheItem(cacheItem))
            .subscribe(
                userInfo -&gt; {
                    if(userInfo != null) showUserInfo();
                    else showPrompt();
                }
            );
  </code></pre>
  
  <p>With RxJava2 I am no longer allowed to post <code>null</code> down the stream, so I either need to wrap my CacheItem into some other class and make my stream produce that wrapper instead or make quite big architectural changes.</p>
  
  <p>Wrapping every single stream element into nullable counterpart doesn't look right to me.</p>
  
  <p>Am I missing something fundamental here? </p>
  
  <p>It seems like the situation like mine is quite popular, so Im curious what is the recommended strategy to tackle this problem given new "no null" policy in RxJava2?</p>
  
  <p><strong>EDIT</strong>
  Please see follow-up conversation in <a href="https://github.com/ReactiveX/RxJava/issues/4644" rel="noreferrer">RxJava GitHub repo</a></p>