崩溃的开始:
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)
这是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();
}
}
这是onDataChange函数的代码。
答案 0 :(得分:0)
阅读此问题:What is a NullPointerException, and how do I fix it?
您在尚未初始化的equals()
对象上使用String
。检查您的代码并确保在调用equals()
之前初始化您的对象,以防止这种情况发生。
将代码包裹在if (stringObject != null)
左右,以便您的应用不会崩溃:
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();
}