user10776303
user10776303

Reputation: 241

onError is not called when Maybe receives null

I am learning how to use Maybe observable. I created the below example. Incase of

Maybe.just(value) I will receive the value in onSuccess callback.

In case of Maybe.just(null), I expected to receive the log from onError. However, according to the posted code, when I run the code, the App crashes and I do not receive any logs. Would you please tell me why I am not receiving any logs from onError()?

code

Maybe.just(null)
            .subscribe(
                    i->Log.i("TAG:", "[onSuccess]: " + i),
                    err->Log.e("TAG", "[ERROR] err.getMessage(): " + err.getMessage()),
                    () -> Log.i("TAG", "[COMPLETED]: ")
         );

Upvotes: 2

Views: 312

Answers (1)

dglozano
dglozano

Reputation: 6607

You can't create a Maybe doing Maybe.just(null). It will throw a NullPointerException, that's why your app crashes.

Some people suggested that the library should have the option to Create a Maybe from null in RxJava, but unfortunately that won't happen:

Closing as won't happen. Static methods returning RxJava types can live on any external class.

A workaround for doing that, is to replace this:

Maybe.just(null)

For this:

Maybe.fromCallable(() -> null)

Upvotes: 2

Related Questions