我在从firebase数据库读取数据时遇到错误,我正在尝试允许使用MAC地址登录:
错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
数据:
这是我的代码:
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
答案 0 :(得分:1)
我刚发现错误,我更改了这一行:
Query query = users.orderByChild("macAddress").equalTo(getMacAddr());
到
Query query=users.child(auth.getCurrentUser().getUid()).orderByChild("email");
auth是FirebaseAuth对象
答案 1 :(得分:0)
假设问题确实在您提供的代码中并且给出了错误消息
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
我认为问题出在这一行:
Query query = users.orderByChild("macAddress").equalTo(getMacAddr());
更具体地说,似乎users.orderByChild("macAddress")
正在返回null
。我将研究如何在Firebase数据库中定义users
条目并确保其结构正确。也许您可以尝试在问题行上放置断点并检查users
对象以验证users.orderByChild("macAddress")
是否未返回null
并根据需要对Firebase数据库进行更正。