-2

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

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

david
  • 1
  • 2

1 Answers1

0

Try do this:

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

But it looks like something is wrong in your response object, mb you are not getting response at all? You can also try placing messages in your code like:

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

when usb debugging and see this message in Logcat tab in android studio.

10101101
  • 193
  • 1
  • 13
  • Hi, I did this, but the error was not fixed – david Jan 25 '19 at 17:23
  • Then check body() method it looks it returns null – 10101101 Jan 25 '19 at 17:29
  • this is my class @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; } } – david Jan 25 '19 at 17:36
  • Do you have a way to solve this problem? – david Jan 25 '19 at 17:57
  • Mb try this: if(response.body() != null && response.body().getError() == 0) { ... } – 10101101 Jan 25 '19 at 18:05
  • I did this but now i facing the diffrent error java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.barbershop.models.LoginResponse.getMessage()' on a null object reference at com.example.barbershop.activities.LoginActivity$1$1.onResponse(LoginActivity.java:62) thanks for your help – david Jan 25 '19 at 18:34
  • I edited my post. but looks like this nullpointer is not your real problem... looks like you not get response... or response object is not refreshed – 10101101 Jan 25 '19 at 19:11
  • Sorry, I did not understand what you mean. Thank you for explaining my code – david Jan 25 '19 at 19:17
  • Mb edit your post and paste this body() method, mb there is some error... – 10101101 Jan 25 '19 at 19:17
  • I paste this code in android studio and run now I didnt facing the error nut I dont login to server – david Jan 25 '19 at 19:20
  • Cant help if dont show body() code of Response class – 10101101 Jan 25 '19 at 19:29
  • I edit my post and upload the response class link and the LoginResponse class thanks for your help – david Jan 25 '19 at 19:51
  • There is a way to this problem? – david Jan 25 '19 at 20:50
  • Yep debug it and see what is wrong – 10101101 Jan 25 '19 at 21:23
  • thanks for your help but it is not working – david Jan 26 '19 at 15:09
  • David, have you looked at this line: ExecutorCallAdapterFactory.java:70, line number 70 of the class ExecutorCallAdapterFactory ? Mb this factory does not create some object properly... – 10101101 Jan 26 '19 at 18:39
  • hello 10101101 this class in dosent exist in my code – david Jan 27 '19 at 14:03