-2
Attempt to invoke virtual method java.lang.String com.educosoft.educosoftjamaica.model.BaseResponse.getCountry()' on a null object reference


18680-18680/com.educosoft.educosoftjamaica E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.educosoft.educosoftjamaica, PID: 18680
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.educosoft.educosoftjamaica.model.BaseResponse.getCountry()' on a null object reference
        at com.educosoft.educosoftjamaica.activity.LoginActivity$1.onResponse(LoginActivity.java:80)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:7000)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

My Api interface

public interface ApiInterface {


    @Headers("Content-Type: application/json")
    @POST("/api/EducoUser/VerifyLogin")
    Call<BaseResponse<LoginResponse>> getUser(@Body Map<String, String> params);

}

My Base Response

import com.googl

e.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class BaseResponse<T> {

    @SerializedName("country")
    @Expose
    private String country;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("response")
    @Expose
    private T response;

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public T getResponse() {
        return response;
    }

    public void setResponse(T response) {
        this.response = response;
    }
}

My LoginActivity where i m getting error....

public class LoginActivity extends BaseActivity  {

    ApiInterface apiInterface;

    @BindView(R.id.et_email)
    EditText et_email;

    @BindView(R.id.et_password)
    EditText et_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        ButterKnife.bind(this);
        apiInterface = Appconstant.getApiInterface();

        //String country = getApplicationContext().getResources().getConfiguration().locale.getDisplayCountry();

    }


    @OnClick(R.id.btn_server_login)
    public void onServerLoginClick() {
        String email = et_email.getText().toString();
        String password = et_password.getText().toString();

        hideKeyboard();

      if (email == null || email.isEmpty()) {

            onError(R.string.empty_email);
            return;
        }
        if (!CommonUtils.isEmailValid(email)) {

            onError(R.string.invalid_email);
            return;
        }
        if (password == null || password.isEmpty()) {

            onError(R.string.empty_password);
            return;
        }

        showLoading();

        Map<String, String> params = new HashMap<String, String>();
        params.put("Email", email);
        params.put("Password", password);


        apiInterface.getUser(params).enqueue(new Callback<BaseResponse<LoginResponse>>() {
            @Override
            public void onResponse(Call<BaseResponse<LoginResponse>> call, Response<BaseResponse<LoginResponse>> response) {
                hideLoading();
                if (response.body().getCountry().equals("IN")) {
                    onError(response.body().getMessage());
                    onError(response.body().getStatus());
                } else {
                    LoginResponse loginResponse = response.body().getResponse();
                    Appconstant.emailId = loginResponse.getEmail();
                    Appconstant.passwordId = loginResponse.getPassword();
                    onError(response.body().getMessage());
                    onError(response.body().getStatus());

                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    //Intent intent = MainActivity.getStartIntent(LoginActivity.this);
                    //startActivity(intent);

                }
            }

            @Override
            public void onFailure(Call<BaseResponse<LoginResponse>> call, Throwable t) {
                hideLoading();
                onError(R.string.some_error);
            }
        });
    }
}
Jens
  • 67,715
  • 15
  • 98
  • 113
  • `response.body()` can return null . You need to check for `null` before proceeding. – ADM Feb 05 '19 at 10:47

1 Answers1

0

It means response.body()is null...

try to check if(response.body() != null) before try to access getCountry()

Thomas Salandre
  • 817
  • 3
  • 14
  • 27