0

Issue with the response.body().string()

Searched all the post in the stackoverflow.
Cannot solve this issue.

 private void signIn() {
    Call<ResponseBody> call = ApiClient.getUserService().createUser(client_fullname, email, nationality, phone, dob, password, cpassword);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            System.out.println("The value of response is" + response.isSuccessful());

            ResponseBody result = response.body();
            try {
                myapiResult = result.string();
            } catch (IOException e) {
                e.printStackTrace();
            }

  
  if (response.isSuccessful()) {
                clearEditText();
                showAlertDialogBox("Sucessfully Submitted", "Thanks for believing us.", 1);
            }


          else if (myapiResult.equals(email)) {
                showAlertDialogBox("Error", "Already Registered.Please Login", 1);

            }
Hubby Tiwari
  • 313
  • 6
  • 14
  • Your response.body() is returning null. Looks like API call is Unsuccessful. Check the API response, and share the Logcat of this call if possible – Samin Jun 15 '21 at 06:54
  • @Samin yes u are right, the logcat error is::D/OkHttp: {"status":false,"message":"Email already exists"} <-- END HTTP (49-byte body) D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: soanitech.n.travelApp, PID: 12800 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String okhttp3.ResponseBody.string()' on a null object reference at soanitech.n.travel.SignUpActivity$4.onResponse(SignUpActivity.java:339) – Hubby Tiwari Jun 15 '21 at 07:04
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – a_local_nobody Jun 15 '21 at 07:33
  • **DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question. [ask] – Rob Jun 15 '21 at 10:01

1 Answers1

1

Your response is null first check it to avoid crash.

if(response.body() != null) {
        String s = response.body().string();
    }

Then you need to send proper response from back end to avoid body to be null.

Muhammad Zahab
  • 1,049
  • 10
  • 21