Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试调用虚方法空点_Java_Android - Fatal编程技术网

Java 尝试调用虚方法空点

Java 尝试调用虚方法空点,java,android,Java,Android,我的Api接口 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

我的Api接口

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)
我的基本反应

导入com.googl

public interface ApiInterface {


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

}
我的登录活动出现错误

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;
    }
}
这意味着response.body为空


尝试检查ifresponse.body!=在尝试访问getCountry

response之前返回null。正文可以返回null。在继续之前,您需要检查null。您将获得null值
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);
            }
        });
    }
}