As explained in the docs RxJava 2.x no longer accepts null values. So it is not surprising that both of the following two lines terminate with onError
called:
Observable.fromCallable(() -> null);
Observable.just(1).flatMap(i -> Observable.error(new RuntimeException()));
what is unclear is why
Observable.just(1).flatMap(i -> Observable.fromCallable(() -> null))
terminates with success and no items emitted. It seams reasonable to expect for it behave in the same fashion as Observable.error
I can see in source code of rx-java 2.1.2
public final <R> Observable<R> flatMap(...) {
if (this instanceof ScalarCallable) {
@SuppressWarnings("unchecked")
T v = ((ScalarCallable<T>)this).call();
if (v == null) {
return empty();
}
...
}
which explains why it is happening in terms of code, but I still have two questions:
1) Is this is an intended behavior or a bug?
2) If intended, is there a reason for this?
This is a bug with the
Observable.fromCallable
and will be fixed with PR 5517.If, for some reason you can't avoid a null return in this setup, you can apply
hide()
to workaround this bug:or help RxJava throw: