5

In my application i have been using retrofit for my webservice calls. It is working fine but when application goes in to background it crashes and got the error log as,

  java.lang.NullPointerException: Attempt to invoke interface method 'void    retrofit.Callback.failure(retrofit.RetrofitError)' on a null object   reference
    at retrofit.CallbackRunnable$2.run(CallbackRunnable.java:53)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:5942)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)


Can anyone help me to overcome this issue..??

This is my Retrofit client class,

public class RetrofitClient {
private static RetrofitCommonService RETROFIT_CLIENT;
public RetrofitClient() {}
public static RetrofitCommonService getInstance() {
    //if REST_CLIENT is null then set-up again.
    if (RETROFIT_CLIENT == null) {
        setupRestClient();
    }
    return RETROFIT_CLIENT;
}

private static void setupRestClient() {
    RestAdapter restAdapter = new RestAdapter.Builder()
            .setClient(new OkClient(new OkHttpClient()))
            .setEndpoint(Constants.BASE_URL)
            //.setLogLevel(RestAdapter.LogLevel.FULL)
            .build();
    RETROFIT_CLIENT = restAdapter.create(RetrofitCommonService.class);
}
}

This is my Retrofit Service interface,

 public interface RetrofitCommonService {

@FormUrlEncoded
@POST("/user")
void doRegistration(@Field("user[mobile_number]") String phone, @Field("user[new_uid]") String imei,
                    Callback<RetrofitResponse> response);

 }

And this way i am making a call to Retrofit service from my activity and activity implementing retrofit callbacks.

 RetrofitCommonService mRetrofitCommonService = RetrofitClient.getInstance();
    mRetrofitCommonService.doRegistration(mPhoneNumber, getDeviceId(), this);

public class RegistrationActivity extends Activity implements Callback<RetrofitResponse>{

}

@Override
public void success(RetrofitResponse retrofitResponse, Response response) {

      }

@Override
public void failure(RetrofitError error) {
}  
3
  • You provided not much more than the error message. It says that the Callback object was null. So make sure you do asynchronous calls always with a valid Callback. Commented Oct 16, 2015 at 5:34
  • put some code here this is too vague Commented Oct 16, 2015 at 5:38
  • Ok i will post my code here Commented Oct 16, 2015 at 5:42

2 Answers 2

3

Fixed this issue.

In my activity i have two Retrofit services operations and these two services relay on same Retrofit Callback methods. And these causes null pointer when one service try to get a callback at the same time callback is used by other service. I have removed the callback methods and used return objects from Retrofit service. Then relaying on the callback method is not needed.

This worked for me...

Sign up to request clarification or add additional context in comments.

1 Comment

We have the same problem according to Google Play crash reports. How can we regenerate the case (situation) on development environment. Any suggested steps?
1

Callback should be properly initialized before calling the method that invokes the callback.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.