I'm facing an error while reading data from firebase DB, I'm trying to allow login with MAC address :
Error:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
Data:
This is my code :
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() is a function that returns mac address of the device in a string
User.class code used in above code :
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;
}
}
this is User.class that is used to create user object
I just found the error , i changed this line :
Query query = users.orderByChild("macAddress").equalTo(getMacAddr());
to
Query query=users.child(auth.getCurrentUser().getUid()).orderByChild("email");
auth is FirebaseAuth object
Assuming that the issue is indeed within the code that you have provided and given the error message
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
I would think that the problem is on this line:
Query query = users.orderByChild("macAddress").equalTo(getMacAddr());
More specifically, it appears that users.orderByChild("macAddress")
is returning null
. I would look into how the users
entry is defined in the Firebase database and make sure it is structured correctly. Maybe you can try placing a breakpoint on the problem line and examine the users
object to verify that users.orderByChild("macAddress")
is not returning null
and make corrections to the Firebase db as needed.
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.