-2

Beginning of crash:

01-12 10:37:17.953 6865-6865/c.mycompany.cart E/AndroidRuntime: FATAL EXCEPTION: main
                                                            Process: c.mycompany.cart, PID: 6865
                                                            java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                at c.mycompany.cart.SignIn$1$1.onDataChange(SignIn.java:63)
                                                                at com.google.android.gms.internal.zzbpx.zza(Unknown Source:13)
                                                                at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source:2)
                                                                at com.google.android.gms.internal.zzbra$1.run(Unknown Source:63)
                                                                at android.os.Handler.handleCallback(Handler.java:789)
                                                                at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                at android.os.Looper.loop(Looper.java:164)
                                                                at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Here is the logcat.

public void onDataChange(DataSnapshot dataSnapshot) {

                    //Check if user dosen't exist in database
                    if (dataSnapshot.child(edtPhone.getText().toString()).exists()) {
                        //Get user information

                        mDialog.dismiss();

                        User user = dataSnapshot.child(edtPhone.getText().toString()).getValue(User.class);
                        if (user.getpassword().equals(edtPassword.getText().toString())) {

                                Intent homeIntent = new Intent(SignIn.this,Home.class);
                                Common.currentUser=user;
                                startActivity(homeIntent);
                                finish();
                            }
                         else {
                            Toast.makeText(SignIn.this, "Incorrect Password !!!", Toast.LENGTH_SHORT).show();
                        }
                    }
                    else
                    {
                        mDialog.dismiss();
                        Toast.makeText(SignIn.this, "User dosen't exist in Database", Toast.LENGTH_SHORT).show();
                    }

                }

Here is the code of the onDataChange function.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

Read this question: What is a NullPointerException, and how do I fix it?

You are using equals() on a String object that has not been initialized yet. Check your code and prevent this from happening by making sure your object is initialized before equals() is called.

Wrap your code around if (stringObject != null) so that your app doesn't crash:

if (user.getpassword() != null) {
    if(user.getpassword().equals(edtPassword.getText().toString())) {
        Intent homeIntent = new Intent(SignIn.this,Home.class);
        Common.currentUser=user;
        startActivity(homeIntent);
        finish();
        }else {
            Toast.makeText(SignIn.this, "Incorrect Password !!!", Toast.LENGTH_SHORT).show();
        }
}else {
    Toast.makeText(SignIn.this, "getpassword returned null", Toast.LENGTH_SHORT).show();
}
Pulak
  • 768
  • 7
  • 16