Android Studio中的nullpoint

时间:2019-01-25 15:57:55

标签: java android

当我在Android Studio上运行此代码并选择“登录”按钮时,遇到以下错误:

Process: com.example.barbershop, PID: 26295
java.lang.NullPointerException: Attempt to invoke virtual method 'int com.example.barbershop.models.LoginResponse.getError()' on a null object reference
    at com.example.barbershop.activities.LoginActivity$1$1.onResponse(LoginActivity.java:53)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)

这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    final EditText emailEditText = findViewById(R.id.input_email);
    final EditText passwordEditText = findViewById(R.id.input_password);
    Button signInButton = findViewById(R.id.btn_signin);
    final SharedPreferences sharedPreferences = getSharedPreferences(MainActivity.LOGIN_SHARED_PREF,MODE_PRIVATE);


    signInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
            progressDialog.setMessage("Authenticating...");
            progressDialog.show();
            String email = emailEditText.getText().toString();
            String password = passwordEditText.getText().toString();
            // Login
            ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
            Call<LoginResponse> loginCall = apiInterface.login(email,password);
            loginCall.enqueue(new Callback<LoginResponse>() {
                @Override
                public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                    progressDialog.hide();
                    if (response.body().getError() == 0){
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        intent.putExtra(MainActivity.SPOT_ID_KEY, response.body().getId());
                        sharedPreferences.edit().putInt(MainActivity.SPOT_ID_KEY, response.body().getId()).apply();
                        startActivity(intent);
                    }


                    else {
                        Toast.makeText(LoginActivity.this, response.body().getMessage(), Toast.LENGTH_LONG).show();
                    }
                }

我想连接到服务器并登录,但是我目前遇到此错误。

这是一个LoginResponse类代码

@SerializedName("error")
public int error;
@SerializedName("message")
private String message;
@SerializedName("id")
private int id;

public int getError() {
    return error;
}

public int getId() {
    return id;
}

public String getMessage() {
    return message;
}

public void setError(int error) {
    this.error = error;
}

public void setId(int id) {
    this.id = id;
}

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

这是响应类代码的链接

http://television.uk.nf/response.txt

任何帮助将不胜感激, 谢谢

http://television.uk.nf/response.txt

1 个答案:

答案 0 :(得分:0)

尝试执行以下操作:

if(response.body() != null) {
    if (response.body().getError() == 0){
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        intent.putExtra(MainActivity.SPOT_ID_KEY, response.body().getId());
        sharedPreferences.edit().putInt(MainActivity.SPOT_ID_KEY, response.body().getId()).apply();
        startActivity(intent);
    }


    else {
        Toast.makeText(LoginActivity.this, response.body().getMessage(), Toast.LENGTH_LONG).show();
    }
}

但是您的响应对象似乎出了点问题,mb您根本没有得到响应吗?您也可以尝试在代码中放置消息,例如:

System.out.println("#########    some message here    ###########")

在进行USB调试时,在android studio的Logcat标签中看到此消息。