Search code examples
androidretrofit2rx-java2

Trouble call fails with Attempt to invoke interface method 'java.lang.reflect.Type retrofit2.CallAdapter.responseType()' on a null object reference


I'm having trouble doing a retrofit request that returns a simple Call object.

I have my retrofit setup done this way:

 new Retrofit.Builder()
                .addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson));

And I have the following retrofit interface which has mixed rxjava and simple calls defined

MyInterface {
   Single getSingle1();
   Single getSingle2();
   Call getCall1();
}

The issue that I face is when I execute: myInterface.getCall1().execute() I get the following error

Attempt to invoke interface method 'java.lang.reflect.Type retrofit2.CallAdapter.responseType()' on a null object reference

Should be noted that if I convert Call getCall1() to return Completable getCall1() everything works as expected.

Anyone has any idea of what I'm doing wrong ?


Solution

  • I'm assuming RxErrorHandlingCallAdapterFactory is the class mentioned in this post.

    Implementations of CallAdapterFactory should return null for a type it doesn't know how to adapt. The wrapped RxJava2CallAdapterFactory does this correctly, and returns null for Call return types (which are normally handled by the built-in DefaultCallAdapterFactory). However, RxErrorHandlingCallAdapterFactory will happily ignore this and return an adapter that delegates to null, causing a crash when you actually try to get a Call.

    To correctly indicate calls it can't handle, your CallAdapterFactory should return null whenever the wrapped factory returns null:

      public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
          CallAdapter wrapped = original.get(returnType, annotations, retrofit);
          if (wrapped = null) {
              return null;
          }
    
          return new RxCallAdapterWrapper(retrofit, wrapped);
      }