Reputation: 201
I created an application that helps users login to an online server using APIs. I use retrofit to help me make network calls to the server. Strangely some devices like tablets and apps like blue stacks gets null pointer exception when the response from the server is null. Kindly help me fix the null pointer exception. NB: There are so many questions on Null pointer Exception but none is tailored to fit my question.
This is my codes below
public void loginUser(String userName, String userPassword, Boolean userRememberMe) {
Retrofit retrofit = RetrofitClient.getClient(authUser.getToken());
APIService mAPIService = retrofit.create(APIService.class);
final ProgressDialog loading = ProgressDialog.show(this, "Please Wait", "...", false, false);
loading.show();
mAPIService.loginUser(userName, userPassword, userRememberMe).enqueue(new Callback<LendingResponse>() {
@Override
public void onResponse(Response<LendingResponse> response, Retrofit retrofit) {
if(response.isSuccess()) {
String userId = response.body().getData().getId();
String userName = response.body().getData().getUsername();
String name = response.body().getData().getName();
String phoneNumber = response.body().getData().getPhoneNumber();
String email = response.body().getData().getEmail();
String token =response.body().getData().getToken();
String gender = response.body().getData().getGender();
loading.dismiss();
}
}
@Override
public void onFailure(Throwable throwable) {
Log.e(TAG, throwable.getMessage());
Toast.makeText(LoginActivity.this, "Unable to Login", Toast.LENGTH_SHORT).show();
loading.dismiss();
}
});
}
This is my error logs from the play store below:
java.lang.NullPointerException:
at com.jonathan.lendingsquare.auth.LoginActivity$3.onResponse (Unknown Source)
at retrofit.ExecutorCallAdapterFactory$ExecutorCallback$1.run (Unknown Source)
at android.os.Handler.handleCallback (Handler.java:733)
at android.os.Handler.dispatchMessage (Handler.java:95)
at android.os.Looper.loop (Looper.java:136)
at android.app.ActivityThread.main (ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative (Native Method)
at java.lang.reflect.Method.invoke (Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:609)
at dalvik.system.NativeStart.main (Native Method)
Upvotes: 0
Views: 1599
Reputation: 3242
This worked for me
if (response.isSuccessful() && response.body() != null) {
//Code here
}
Upvotes: 0
Reputation: 1151
Just check the value null
or not:
if (!(signUp_jsonObject.isNull(response.body().getData().getId())))
{
String userId = response.body().getData().getId();
}
Upvotes: 0
Reputation: 1802
Remove progress dialog than test it.
Other wise check following null or not
boolean mSuccess = false;
if (response.isSuccessful())
if (response.body() != null)
if(response.body().getStatus().getSuccess().equalsIgnoreCase(Constants.getInstance().True))
mSuccess = true;
if (mSuccess) {
//your code`enter code here`
}
Upvotes: 3