title: NullPointerException using Retrofit response
body: <p>I'm using Retrofit to communicate with REST API on Android, but I'm
  getting a NullPointerException like below. I try using Postman,
  the API is working fine and I get the response the login was working.</p>
  
  <p><strong>Error</strong></p>
  
  <blockquote>
    <p>Process: com.example.krish.webdemo, PID: 3064
        java.lang.NullPointerException
            at com.example.krish.webdemo.activities.LoginActivity$2.onResponse</p>
  </blockquote>
  
  <pre><code> Call&lt;LoginResponse&gt; call = RetrofitClient
                  .getInstance()
                  .getApi()
                  .userLogin(email, password);
  
          call.enqueue(new Callback&lt;LoginResponse&gt;() {
              @Override
              public void onResponse(Call&lt;LoginResponse&gt; call, Response&lt;LoginResponse&gt; response) {
                  LoginResponse loginResponse = response.body();
                  if (!loginResponse.isError()) {
                      Toast.makeText(LoginActivity.this, loginResponse.getMessage(), Toast.LENGTH_LONG).show();
                  } else {
                      Toast.makeText(LoginActivity.this, loginResponse.getMessage(), Toast.LENGTH_LONG).show();
                  }
              }
  
              @Override
              public void onFailure(Call&lt;LoginResponse&gt; call, Throwable t) {
  
              }
          });
      }
  </code></pre>
  
  <p>I had a <code>NullPointerException</code> to my loginResponse, the '.isError()' was
  showing null error</p>
  
  <p><strong>API interface</strong></p>
  
  <pre><code>@FormUrlEncoded
      @POST("userlogin")
      Call&lt;LoginResponse&gt; userLogin(
              @Field("email") String email,
              @Field("password") String password
      );
  </code></pre>
  
  <p><strong>Retrofit instance</strong></p>
  
  <pre><code>private static final String BASE_URL ="http://192.168.1.38/KrishApi/public/";
      private static RetrofitClient mInstance;
      private Retrofit retrofit;
  
      private RetrofitClient() {
  
  
          retrofit = new Retrofit.Builder()
                  .baseUrl(BASE_URL)
                  .addConverterFactory(GsonConverterFactory.create())
                  .build();
      }
  
      public static synchronized RetrofitClient getInstance() {
          if (mInstance == null) {
              mInstance = new RetrofitClient();
          }
          return mInstance;
      }
      public Api getApi(){
          return retrofit.create(Api.class);
      }
  </code></pre>
  
  <p><strong>JSON response</strong></p>
  
  <pre><code>{
      "error": false,
      "message": "Login Successful",
      "user": {
          "id": 3,
          "email": "pandi@gmail.com",
          "name": "pandi",
          "age": "22",
          "college": "sec"
      }
  }
  </code></pre>
  
  <p><strong>POJO class</strong></p>
  
  <pre><code>public class LoginResponse {
  
  
      private boolean error;
      private String message;
      private User user;
  
      public LoginResponse(boolean error, String message, User user) {
          this.error = error;
          this.message = message;
          this.user = user;
      }
  
      public boolean isError() {
          return error;
      }
  
      public String getMessage() {
          return message;
      }
  
      public User getUser() {
          return user;
      }
  }
  </code></pre>