尝试调用虚拟方法空点

时间:2019-02-05 10:44:34

标签: java android

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)

我的Api界面

public interface ApiInterface {


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

}

我的基本回复

导入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;
    }
}

我的LoginActivity出现错误。...

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);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

这意味着response.body()为空...

尝试访问if(response.body() != null)之前先检查getCountry()