Dealing with void and null in RxJava 2

By Patryk Lenza | May 20, 2018

RxJava 2 void and null

The Problem

I recently updated my RxJava dependency from version 1.x to 2.x. Everything looks very good in RxJava world - new version seems to be more coherent and consistent. Much cleaner. You can find large amount of details in an official RxJava Github Repo. First sentences on this page say that:

RxJava 2.0 has been completely rewritten from scratch on top of the Reactive-Streams specification. The specification itself has evolved out of RxJava 1.x and provides a common baseline for reactive systems and libraries.

Because Reactive-Streams has a different architecture, it mandates changes to some well known RxJava types.

So there are some major changes. The one that surprised me was related to returning null values and dealing with methods that return void. The doc says:

RxJava 2.x no longer accepts null values and will yield NullPointerException immediately or as a signal to downstream.

This is actually very good design and I totally support it. Java world is plagued by nulls and NPEs and everything that avoids falling into such traps is more than welcome.

But it made my existing and typical RxJava 1.x code fail. For example I was wrapping some synchronous method that returned void into Observable:

public Observable<Void> makeMeAnObservable(String someParam) {
    return Observable.fromCallable(() -> {
        someMethodThatReturnsVoid(someParam);
        return null;
    });
}
Basically, I have synchronous method that returns null and I want to wrap it in an Observable. I don’t care about result, I just want to fire the method and wait for its completion or error. I can create Observable of Void or just of Object and return null. Subscriber then shall ignore resulting element. However it won’t work in RxJava 2 - we will get NPE.

The Solution

The solution I ended up with uses Completable - it is present since version 1.x I just somehow never found it. JavaDoc for this class states Represents a deferred computation without any value but only indication for completion or exception and this is exactly what we need. It has a bunch of static creation methods like fromRunnable, fromAction, fromCallable and more. All we need to do is to wrap our original method into Completable just like this:

public Completable makeMeCompletable(String someParam) {
    return Completable.fromRunnable(() -> someMethodThatReturnsVoid(someParam));
}
And usage is identical like with an Observable but we just don’t need to deal with return value:
makeMeCompletable("param").subscribe(() -> {}, throwable -> {});
Where first param is an Action that will be run on successful completion and second parameter is a Consumer of a Throwable. And that’s it.

I hope that this simple and short post will help you when dealing with void and null in RxJava 2. Thanks!

Tactical Support From A Veteran Team

Partner with our experienced, full-stack development, self-organized team to design and develop a tailored product or improve your existing solution. We are engineers, but we always reach for more - we are the team of full-stack employees.

Let's talk
comments powered by Disqus