Kamil Radz
Kamil Radz

Reputation: 47

E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-3 Callable returned a null value

Inside my Activity B I call an async method called myASYNC that gets data from an API (the amount of data is big enough that it takes a few seconds to load all of it). If I click the Android back button that takes me back to activity A while the myASYNC is still running I get this error and the app crashes:

 E/AndroidRuntime: FATAL EXCEPTION: RxCachedThreadScheduler-3
    Process: com.hfad.crypto, PID: 9473
    java.lang.NullPointerException: Callable returned a null value. Null values are generally not allowed in 3.x operators and sources.

This happens only if I exit Activity "B" while my method is running even though it should get Disposed:

@Override
    protected void onDestroy() {
        super.onDestroy();
        if(disposableMain != null && !disposableMain.isDisposed()){
            disposableMain.dispose();
            Log.d("Disposable", "disposed");
        }
}

Activity "B"

public class ActivityB extends AppCompatActivity {
    private Disposable disposableMain;
 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        myASYNC("DATA");
}

private void myASYNC(String data){
  Observable.fromCallable(() -> {
            try {
                URL url1 = new URL(data);
                HttpURLConnection connection = (HttpURLConnection) url1
                        .openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                return BitmapFactory.decodeStream(input);
            } catch (IOException e) {
                e.printStackTrace();
                Log.d("JSONTask failed", "task failed" + e.toString());
                return null;
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new io.reactivex.rxjava3.core.Observer<Bitmap>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                disposableMain = d;
            }
            @Override
            public void onNext(@NonNull Bitmap bitmap) {
                bitmapList.add(bitmap);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onComplete() {}
        });
}


@Override
    protected void onDestroy() {
        super.onDestroy();
        if(disposableMain != null && !disposableMain.isDisposed()){
            disposableMain.dispose();
            Log.d("Disposable", "disposed");
        }
}}

How do I prevent this?

Upvotes: 1

Views: 929

Answers (1)

laalto
laalto

Reputation: 152927

As the exception says, you're returning a null from your Callable.

First, remove the try-catch construct that returns a null from the catch block. You can handle exceptions in your observer's onError. (Note that the logging about "JSONTask" is also misleading.)

Second, BitmapFactory.decodeStream() can also fail and return null. You can turn that into an exception with e.g. wrapping it in Objects.requireNonNull().

Upvotes: 3

Related Questions