我在Firebase中有以下数据:
(我不允许在Stackoverflow中附加图片):
有一个根节点 - > root有一个名为" NewStaffRequests"的子节点。 " NewStaffRequests"有多个用户具有唯一ID。在每个唯一ID下,一个特定用户'存在email-id和角色密钥。 '角色' key可以包含诸如" Trainer"。
之类的值电子邮件ID已在auth系统中注册。将来,会有更多此类用户具有唯一ID,电子邮件ID和角色。我想登录,检索用户的角色,该用户的电子邮件ID是从活动中的电子邮件ID TextView获取的,在" NewStaffRequests"中找到此电子邮件ID。并获得该用户的角色。我为此编写了以下代码。但是,对象' StaffUser值'只获得null。请指教!
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_staff_login);
mAuth = FirebaseAuth.getInstance();
heading = (TextView) findViewById(R.id.heading);
note = (TextView) findViewById(R.id.note);
email = (EditText) findViewById(R.id.enterEmail);
pwd = (EditText) findViewById(R.id.enterPassword);
spinner = (Spinner) findViewById(R.id.roles_spinner);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
final String staffEmail = email.getText().toString().trim();
final String password = pwd.getText().toString().trim();
if(TextUtils.isEmpty(staffEmail))
{
Toast.makeText(getApplicationContext(), "You must enter your E-Mail!", Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password))
{
Toast.makeText(getApplicationContext(), "You must enter your Password!", Toast.LENGTH_SHORT).show();
return;
}
}
mAuth.signInWithEmailAndPassword(staffEmail,password).addOnCompleteListener(StaffLogin.this, new OnCompleteListener<AuthResult>()
{
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
Log.d(TAG, "successfull login : " + task.isSuccessful());
if (!task.isSuccessful()){
Toast.makeText(StaffLogin.this, "Authentication failed." + task.getException(), Toast.LENGTH_LONG).show();
}
else {
DatabaseReference root = FirebaseDatabase.getInstance().getReference();
Query reference = root.child("NewStaffRequests").orderByChild("email").equalTo(staffEmail);
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getChildrenCount() > 0) {
StaffUser value = dataSnapshot.getValue(StaffUser.class);
String staffRole = value.role;
if(staffRole.equals("Fitness Trainer"))
{
startActivity(new Intent(StaffLogin.this, RMLanding.class));
finish();
}
else
{
Toast.makeText(getApplicationContext(), "The user is someone else", Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
});
}
});
}
}
以下是StaffUser类:
public class StaffUser {
public String email, name, role;
public Boolean approvalStatus;
public StaffUser() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public StaffUser(String _email, String _name,String _role,Boolean _approvalStatus) {
this.email = _email;
this.name = _name;
this.role = _role;
this.approvalStatus = _approvalStatus;
}
}
以下是错误消息:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference.
此错误指向的行是:
String staffRole = value.role;
亚历克斯的建议后所做的更改:
mAuth.signInWithEmailAndPassword(staffEmail,password).addOnCompleteListener(StaffLogin.this, new OnCompleteListener<AuthResult>()
{
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{
Log.d(TAG, "successfull login : " + task.isSuccessful());
if (!task.isSuccessful()){
Toast.makeText(StaffLogin.this, "Authentication failed." + task.getException(), Toast.LENGTH_LONG).show();
}
else {
DatabaseReference root = FirebaseDatabase.getInstance().getReference();
Query reference = root.child("NewStaffRequests").orderByChild("email").equalTo(staffEmail);
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getChildrenCount() > 0) {
//Toast.makeText(StaffLogin.this, "datasnapshot value" + dataSnapshot.getValue(StaffUser.class), Toast.LENGTH_LONG).show();
StaffUser value = dataSnapshot.getValue(StaffUser.class);//When I attach the debugger on this line, I can see that 'value' is null, but dataSnapshot.getValue(StaffUser.class) is showing the stored database values from firebase.
Log.d(TAG, "datasnapshot value" + value.getRole());
String staffRole = value.getRole();
if(staffRole.equals("Rescue Manager"))
{
startActivity(new Intent(StaffLogin.this, RMLanding.class));
finish();
}
else
{
Toast.makeText(getApplicationContext(), "The user is someone else", Toast.LENGTH_SHORT).show();
}
}
else{
Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
});
答案 0 :(得分:0)
如果您收到此错误java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
,则表示staffRole
字符串为null
。
要解决此问题,您需要使用以下代码行:
String staffRole = value.getRole();
这也意味着您需要在模型类中添加此方法,如下所示:
public String getRole() {
return role;
}
请注意,您需要将公共getter添加到您的所有字段中,并且字段本身必须是私有的,而不是我在课堂上看到的公开。你的课应该是这样的:
public class StaffUser {
private String email, name, role;
private boolean approvalStatus;
public StaffUser() {}
public StaffUser(String email, String name, String role, Boolean approvalStatus) {
this.email = email;
this.name = name;
this.role = role;
this.approvalStatus = approvalStatus;
}
public String getEmail() {return email;}
public String getName() {return name;}
public String getRole() {return role;}
public boolean getApprovalStatus() {return approvalStatus;}
}
要获取实际对象,请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("NewStaffRequests").orderByChild("email").equalTo(staffEmail);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
StaffUser value = dataSnapshot.getValue(StaffUser.class);
Log.d("TAG", value.getRole());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
query.addListenerForSingleValueEvent(eventListener);