5

I hava some problem with RxJava2 and a stream (Observable and Observer) Actually, my stream return an error

The mapper function returned a null value.
io.reactivex.internal.functions.ObjectHelper.requireNonNull(ObjectHelper.java:39)
io.reactivex.internal.operators.observable.ObservableMap$MapObserver.onNext(ObservableMap.java:57)
retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:51)
retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:37)
retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:47)

my logcat doesn't show where the code breaks.


 public static Observable<List<Business.Response.Doc>> streamArticles_Business() {
        NYT_TopStories_services nyt_services = NYT_TopStories_services.retrofit.create(NYT_TopStories_services.class);
        return nyt_services.getArticles_Business()
                .map(Business.Response::getDocs)
                .filter(str -> str != null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .timeout(20, TimeUnit.SECONDS);
    }

and my retrofit Get

 @GET("search/v2/articlesearch.json?q=business&api-key=" + apiKey)
    Observable<Business.Response> getArticles_Business();


    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.nytimes.com/svc/")
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build();


Do you have any way to fix that?

  • 1
    Looks like `Business.Response::getDocs` returns `null`. You have to decide what such null getDocs means for your application. You could filter it out, return a default document or an optional of document. – akarnokd Jul 08 '19 at 14:23
  • Ok, great! thanks a alot. I'm trying to use .filter(str -> str != null) but not enought –  Jul 08 '19 at 15:43
  • If the map fails with the null error, you should filter out null getDocs **before** that. – akarnokd Jul 08 '19 at 18:06
  • 1
    Handling null in RxJava 2.0: https://medium.com/@joshfein/handling-null-in-rxjava-2-0-10abd72afa0b – RefuX May 29 '20 at 15:55

1 Answers1

1

Since it is an internal RxJava crash, you would not get to see that in your code, however if you traverse into the map{} hierarchy, you will understand the reason behind it.

This crash occurs if you are mapping with a null value, basically if the statement becomes this map{null} it will crash. A simple fix for this is to have default values for all the nullable types.

For example :

map{nullableField ?: defaultValue}
doersweb
  • 231
  • 2
  • 9