This is the errror, tried a few things but still to no avail.
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object referenc e at com.dllibrary.cart.SigninMainActivity$1$1.onDataChange( SigninMainActivity.java:56) at com.google.android.gms.internal.zzegf.zza(Unknown Source) at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source) at com.google.android.gms.internal.zzeig.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5637) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
code for my sign in method
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog mdialog = new ProgressDialog(SigninMainActivity.this);
mdialog.setMessage("Please waiting");
mdialog.show();
table_user.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//check if user not exist
if (dataSnapshot.child(phone.getText().toString()).exists()) {
//Get User Information
mdialog.dismiss();
User user = dataSnapshot.child(phone.getText().toString()).getValue(User.class);
if (user.getPassword().equals(password.getText().toString())) {
Intent homeIntent= new Intent(SigninMainActivity.this, home.class);
Common.currentUser = user;
startActivity(homeIntent);
finish();
} else {
Toast.makeText(SigninMainActivity.this, "Sign in failed", Toast.LENGTH_SHORT).show();
}
}else{
mdialog.dismiss();
Toast.makeText(SigninMainActivity.this, "User Does Not exist", Toast.LENGTH_SHORT).show();
}
}
line 56 is at "if(user.getPassword.equals)"
Check the below code I implement with the change I expected.
signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog mdialog = new ProgressDialog(SigninMainActivity.this);
mdialog.setMessage("Please waiting");
mdialog.show();
table_user.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//check if user not exist
if (dataSnapshot.child(phone.getText().toString()).exists()) {
//Get User Information
mdialog.dismiss();
User user = dataSnapshot.child(phone.getText().toString()).getValue(User.class);
//Check the null
if (user.getPassword() != null && password.getText() != null && user.getPassword().equals(password.getText().toString())) {
Intent homeIntent= new Intent(SigninMainActivity.this, home.class);
Common.currentUser = user;
startActivity(homeIntent);
finish();
} else {
Toast.makeText(SigninMainActivity.this, "Sign in failed", Toast.LENGTH_SHORT).show();
}
}else{
mdialog.dismiss();
Toast.makeText(SigninMainActivity.this, "User Does Not exist", Toast.LENGTH_SHORT).show();
}
}
The following error:
Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
Tells you what is all about. It means that in this line of code:
if (user.getPassword().equals(password.getText().toString()))
That exists at line 56
, user.getPassword()
returns null
.
This is happening because the User
object that you are trying to get from the database has the value of null
. So to solve this, make sure you get the correct User
object. Also, check this object for nullity before actually using it:
if(user != null) {}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.