When I run this code on Android Studio and select the login button, I encounter the following error:
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)
and this is my code
@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();
}
}
I want to connect to the server and log in, but I am currently encountering this error.
this is a LoginResponse class code
@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;
}
and this is a link for response class code
http://television.uk.nf/response.txt
Any help would be appreciated, Thanks