0

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)
3
  • Check your api response with same request on web rest client like Postman. Handle the exception with try-catch on response as its always exception prone code block . Commented Sep 7, 2017 at 7:45
  • @AMD it works with postman message body is never null..it is just strange i get null messages when using blue stacks..would surround that block of code with try catch Commented Sep 7, 2017 at 8:18
  • Check the Model class you assign to retrofit . There must be some parsing issue. Commented Sep 7, 2017 at 8:25

3 Answers 3

3

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`
                }
Sign up to request clarification or add additional context in comments.

2 Comments

what is this line checking for if(response.body().getStatus().getSuccess().equalsIgnoreCase(Constants.getInstance().True))
This line checking for you API response success true or false or otherwise 1 or 0 whatever we get result
0

Just check the value null or not:

if (!(signUp_jsonObject.isNull(response.body().getData().getId())))
{ 
    String userId = response.body().getData().getId();
}

3 Comments

if (!signUp_jsonObject.isNull(response.body().getData().getId())) is enough (removed parentheses)
@DanielAlder It's totally depend on your response.In my Code i generally use to check 2 things Whether key is exists and value associated to that key is null or not. if(jsonObject.has("exists")) { if (!(jsonObject.isNull("exists"))) { ----- } }
I don't understand what you mean. I was only saying that if (!(bool)) and if (!bool) are the same
0

This worked for me

if (response.isSuccessful() && response.body() != null) {
       //Code here

}

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.