Android Java查询firebase空指针异常

时间:2017-05-09 07:40:52

标签: android firebase firebase-realtime-database

我正在尝试使用Firebase在Android中执行类似WHERE子句的查询。这是我的代码:

Database d = new Database();
mFirebaseInstance = FirebaseDatabase.getInstance();
usersDatabase = mFirebaseInstance.getReference(d.getUsersTable());
Query queryRef = usersDatabase.orderByChild("email").equalTo(email);
queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot snapshot) {
         user = snapshot.getValue(User.class);
     }

     @Override public void onCancelled(DatabaseError error) { }
});

// credential matches
if(user.getEmail().equals(email) && user.getPassword().equals(password)){
     Intent i = new Intent(getBaseContext(), MainActivity.class);
     startActivity(i);
     finish();
 }else{
     Toast.makeText(LoginActivity.this,"Invalid email or password. Please try again.", Toast.LENGTH_SHORT).show();
 }

但是,我收到以下错误消息:

FATAL EXCEPTION: main
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference 
at com.example.LoginActivity$1.onClick(LoginActivity.java:78)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19888)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5273)
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:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

有什么想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

很明显,您的user对象为空。要解决您的问题,请使用以下代码:

mFirebaseInstance = FirebaseDatabase.getInstance();
usersDatabase = mFirebaseInstance.getReference().child("Users").child(userId);

ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    String email = (String) dataSnapshot.child("email").getValue();
    String password = (String) dataSnapshot.child("password").getValue();

    if(userEmail.equals(email) && userPassword.equals(password)){
        Intent i = new Intent(getBaseContext(), MainActivity.class);
        startActivity(i);
        finish();
    } else {
        Toast.makeText(LoginActivity.this,"Invalid email or password. Please try again.", Toast.LENGTH_SHORT).show();
    }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
usersDatabase.addListenerForSingleValueEvent(eventListener);

其中userIdpush()方法生成的唯一ID。

userEmailuserPassword是您要比较的值。

要将userId从某个活动转移到另一个活动,您可以像这样使用intent

FirstActivity中使用此内容:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("userId", userId);
startActivity(intent);

要让userId回到SecondActivity,请使用此功能:

String userId = (String) getIntent().getExtras().get("userId");

希望它有所帮助。