gpt4 book ai didi

java - Firebase 读取数据时出现空对象引用的 onDataChange 方法错误

转载 作者:行者123 更新时间:2023-12-02 11:27:58 31 4
gpt4 key购买 nike

我在从 Firebase DB 读取数据时遇到错误,我尝试允许使用 MAC 地址登录:

错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

数据:

Data Image

这是我的代码:

 DatabaseReference users = FirebaseDatabase.getInstance().getReference("users");
Query query = users.orderByChild("macAddress").equalTo(getMacAddr());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
User user = dataSnapshot.getValue(User.class);
if(!(user.getEmail().equals(edtEmail.getText().toString()))){
Snackbar.make(rootLayout, "Please Login With Phone You Registered With", Snackbar.LENGTH_SHORT).show();
waitingdialog.dismiss();
return;
}
}
else{
Snackbar.make(rootLayout, "Please Login With Phone You Registered With", Snackbar.LENGTH_SHORT).show();
waitingdialog.dismiss();
return;
}
}

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

getMacAddr() 是一个以字符串形式返回设备 MAC 地址的函数

上面代码中使用的User.class代码:

public class User {

private String email,password,name,phone,macAddress;
private int isSecured;

public User(String email, String name, String password,String phone,String macAddress,int isSecured){

this.email=email;
this.name=name;
this.password=password;
this.phone=phone;
this.macAddress=macAddress;
this.isSecured=isSecured;
}
public User() {}

public int getIsSecured() {
return isSecured;
}

public void setIsSecured(int isSecured) {
this.isSecured = isSecured;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMacAddress() {
return macAddress;
}

public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}
}

这是用于创建用户对象的 User.class

最佳答案

我刚刚发现错误,我更改了这一行:

Query query = users.orderByChild("macAddress").equalTo(getMacAddr());

Query query=users.child(auth.getCurrentUser().getUid()).orderByChild("email");

auth 是 FirebaseAuth 对象

关于java - Firebase 读取数据时出现空对象引用的 onDataChange 方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49481390/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com