0
votes

We are using RxAndroid + retrofit for our api calls.

I sort of understand why any exceptions within the sequence will pass the Throwable through onError().

But how do you end the sequence, how do you then process a response and return to throwing fatal exceptions again? I would have expected processing in onCompleted() would allow this, but I'm still seeing onError() being called.

Simplified snippet below. Gives the result -

throwable: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference

        Observable<ResponseModel> observable = getApiCallObservable();
        AppObservable.bindFragment(this, observable)
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseModel>() {
                    @Override
                    public void onCompleted() {
                        uninitializedList.add("item");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e(TAG, "throwable: " + e.getMessage());
                    }

                    @Override
                    public void onNext(ResponseModel response) {
        
                    }
                });

thanks for any help

1

1 Answers

0
votes

I'm not sure i understood what is being asked here. Basically, if your Observable getApiCallObservable() throws an error, onError will be called and you should handle the error accordingly in onError. If that happens, as it is, nothing will happen anymore. If an error happens, onError is called and stream end. If there is no error, onNext is called, and that's where you should do anything with your response. After onNext, finally, onCompleted is called.

The error that is showing is simply saying that your arraylist uninitializedList is null, so the method call add() is invalid.

Edit. I think i got your point now. You want handle the error without onError being called by the observable. Move your oncomplete code to onNext, and it won't fall under onError, it will throw the fatal exception. is that it?