title: Retrofit response return null in onResponse function (single JSON object)
body: <p>I'm try to get response from retrofit API after register the user.</p>
  
  <p>I get null from <code>response.body()</code> function </p>
  
  <p>Register URL: <a href="https://apitest10101.000webhostapp.com/alaa-apii/public/createuser" rel="nofollow noreferrer">https://apitest10101.000webhostapp.com/alaa-apii/public/createuser</a> (Method POST)</p>
  
  <p>The API return single object JSON like </p>
  
  <pre><code>{
      "error": true,
      "message": "User Already Exists"
  }
  
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/4HZ8h.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4HZ8h.png" alt="enter image description here"></a>"response work after tested in postman"</p>
  
  <p><strong>RegisterActivity.java</strong> </p>
  
  <pre><code>public void register(View view) {
  
         if(Validate())
         {
             Log.d("####", "OKAAAAAY");
  
              first_name = first_name_edit.getText().toString().trim();
              last_name = last_name_edit.getText().toString().trim();
              email = email_edit.getText().toString().trim();
              password = password_edit.getText().toString().trim();
              phone = Integer.parseInt(phone_edit.getText().toString().trim());
              state = state_edit.getText().toString().trim();
              block = block_edit.getText().toString().trim();
              street = street_edit.getText().toString().trim();
              building = buidling_edit.getText().toString().trim();
              floor = floor_edit.getText().toString().trim();
              flat = flat_edit.getText().toString().trim();
  
  
            Call&lt;ResultModel&gt; call = RetrofitClient
                  .getInstance()
                  .getApi()
                  .createUser(first_name, last_name, email, password, phone, postionOfReigon, state, block, street, building, floor, flat);
  
            call.enqueue(new Callback&lt;ResultModel&gt;() {
                @Override
                public void onResponse(Call&lt;ResultModel&gt; call, Response&lt;ResultModel&gt; response) {
  
                    boolean error = false;
                    String mass = null;
                    if (response.body() != null) { // response null  !!!!!!!!!!!
                        error = response.body().getError();
                        mass = response.body().getMessage();
                        Log.d("#####", error + "------" + mass);
                    }
  
  
                        Log.d("#####", error + "------" + mass);
  
                }
  
                @Override
                public void onFailure(Call&lt;ResultModel&gt; call, Throwable t) {
                    Toast.makeText(RegisterActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
  
                }
            });
  
  
         }
         else
         {
             Log.d("####", "NNNOOTT OKAAAAAY");
         }
  
      }
  
  </code></pre>
  
  <p>If I remove if statement in <code>response.body()</code> I got this error in log </p>
  
  <pre><code>2019-11-10 12:45:51.242 21211-21211/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.myapplication, PID: 21211
      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Boolean com.example.myapplication.ResultModel.getError()' on a null object reference
          at com.example.myapplication.RegisterActivity$2.onResponse(RegisterActivity.java:161)
          at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$1$1.run(DefaultCallAdapterFactory.java:83)
          at android.os.Handler.handleCallback(Handler.java:873)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:193)
          at android.app.ActivityThread.main(ActivityThread.java:6718)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
  </code></pre>
  
  <p><strong>ResultModel.java</strong></p>
  
  <pre><code>package com.example.myapplication;
  
  import com.google.gson.annotations.Expose;
  import com.google.gson.annotations.SerializedName;
  
  public class ResultModel {
  
      @SerializedName("error")
      @Expose
      private Boolean error;
      @SerializedName("message")
      @Expose
      private String message;
  
      public ResultModel(Boolean error, String message) {
          this.error = error;
          this.message = message;
      }
  
      public Boolean getError() {
          return error;
      }
  
      public void setError(Boolean error) {
          this.error = error;
      }
  
      public String getMessage() {
          return message;
      }
  
      public void setMessage(String message) {
          this.message = message;
      }
  
  }
  
  </code></pre>
  
  <p><strong>RetrofitClient.java</strong></p>
  
  <pre><code>package com.example.myapplication;
  
  import retrofit2.Retrofit;
  import retrofit2.converter.gson.GsonConverterFactory;
  
  public class RetrofitClient {
  
      private  static final String BASE_URL = "https://apitest10101.000webhostapp.com/alaa-apii/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>API interface</strong></p>
  
  <pre><code>package com.example.myapplication;
  
  
  import retrofit2.Call;
  import retrofit2.http.Field;
  import retrofit2.http.FormUrlEncoded;
  import retrofit2.http.POST;
  
  public interface Api {
  
  
      @FormUrlEncoded
      @POST("createuser")
      Call&lt;ResultModel&gt; createUser(
              @Field("firstname") String firstname,
              @Field("lastname") String lastname,
              @Field("email") String email,
              @Field("password") String password,
              @Field("phone") int phone,
              @Field("region") int region,
              @Field("state") String state,
              @Field("block") String block,
              @Field("street") String street,
              @Field("building") String building,
              @Field("floor") String floor,
              @Field("flat") String flat
      );
  }
  
  </code></pre>
  
  <p><strong>build gradle</strong></p>
  
  <pre><code>implementation 'com.squareup.retrofit2:retrofit:2.6.2'
  implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
  </code></pre>
  
  <p>the <code>getError()</code> should return false or true and the <code>getMessage()</code>
  should return the message <strong>User Already Exists</strong> or <strong>User created successfully</strong> .</p>
  
  <p>I use slim framework v3.12 to build api.</p>
  
  <p>** Any suggest **</p>