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) {
}
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With