title: How to Read response if "response.isSuccessful()" is true in Retrofit
body: <p>JSON Response from API:</p>
  
  <pre><code>{
      "type": "loginResponseMessage",
      "statusCode": 200,
      "statusMessage": "Login Successful",
      "resource_id": 3
  }
  </code></pre>
  
  <p>I want to read JSON response. When 'if(response.isSuccessful())' is executing I am getting null pointer exception. In case of execution of 'else' block I am getting correct response. Following is the code:</p>
  
  <pre><code>@Override
              public void onResponse(Call&lt;Login&gt; call, Response&lt;Login&gt; response) {
              if (response.isSuccessful()) {
                  LoginResponseMessage loginResponseMessage;
  
                  loginResponseMessage = new APIResponse().getResponse2(retrofit,response);
  
                  String toastMessage = "StatusMessage: "+loginResponseMessage.getStatusMessage()
                          +"Status Code: "+loginResponseMessage.getStatusCode()
                          +"ResourceId: "+loginResponseMessage.getResource_id();
                  Toast.makeText(LoginActivity.this,toastMessage, Toast.LENGTH_SHORT).show();
              } else {
  
                  switch(response.code()){
                      case 401:
                          LoginResponseMessage loginResponseMessage;
  
                          loginResponseMessage = new APIResponse().getResponse2(retrofit,response);
  
                          String toastMessage = "StatusMessage: "+loginResponseMessage.getStatusMessage()
                                  +"Status Code: "+loginResponseMessage.getStatusCode()
                                  +"ResourceId: "+loginResponseMessage.getResource_id();
                          Toast.makeText(LoginActivity.this,toastMessage, Toast.LENGTH_SHORT).show();
                          break;
                  }
              }
          }
  </code></pre>
  
  <p>Class APIResponse:</p>
  
  <pre><code>public class APIResponse {
      private String statusMessage;
  
      public LoginResponseMessage getResponse2(Retrofit retrofit, Response response){         //This will only be called in LoginActivity
          LoginResponseMessage responseMessage = new LoginResponseMessage();
  
          Converter&lt;ResponseBody, LoginResponseMessage&gt; converter
                  = retrofit.responseBodyConverter(LoginResponseMessage.class, new Annotation[0]);
          try {
              responseMessage = converter.convert(response.errorBody());
          } catch (IOException e) {
              e.printStackTrace();
          }
          return responseMessage;
      }
  }
  </code></pre>
  
  <p>Same logic is applied in 'if' &amp; 'else' block. But getting null pointer exception in 'if (response.isSuccessful())' block</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Reader okhttp3.ResponseBody.charStream()' on a null object reference
                                                                        at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:35)
                                                                        at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
                                                                        at pk.qareeb.qareeb.LoginActivity$5.onResponse(LoginActivity.java:206)
                                                                        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
                                                                        at android.os.Handler.handleCallback(Handler.java:761)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                        at android.os.Looper.loop(Looper.java:156)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6523)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
  t
  </code></pre>
  
  <p><strong>2nd Approach:</strong>
  I have also tried following code.</p>
  
  <pre><code>@Override
       public void onResponse(Call&lt;Login&gt; call, Response&lt;Login&gt; response) {
           Toast.makeText(LoginActivity.this,response.body().toString(),Toast.LENGTH_LONG).show();
  }
  </code></pre>
  
  <p>By adding 2nd approach code, instead of toasting JSON response, it is toasting "com.qareeb.qareeb.modal.Login@119c611". Why ?</p>