Ibrahim Abousalem Ibrahim Abousalem - 2 months ago 114
Android Question

Why When i get the data From Firebase it return NULL?

I'm trying to retrieve user's data from firebase using search by Email to find the user ID and use that ID to get the user's node children so i used that code to do that:

mSignin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

mEmailStr = SignupActivityFragment.removeSpaces(mEmail.getText().toString());
mPasswordStr = mPassword.getText().toString();

mAuth.signInWithEmailAndPassword(mEmailStr, mPasswordStr).addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(Task<AuthResult> task) {
if (task.isSuccessful()){
getExistingUserInfo();
if(User!=null) {
sglstr = User.get("sgl");
if (sglstr.equals("true")) {
Intent intent = new Intent(getActivity(), ControllerSGL.class);
intent.putExtra("user", User);
startActivity(intent);
} else if (sglstr.equals("false")) {
Intent intent = new Intent(getActivity(), ControllerStudent.class);
intent.putExtra("user", User);
startActivity(intent);
}

}
else {
Toast.makeText(getActivity(), "NULL!!!!!!", Toast.LENGTH_SHORT).show();
}
Toast.makeText(getActivity(), "Success", Toast.LENGTH_SHORT).show();

}else {
Toast.makeText(getActivity(), "Not Success", Toast.LENGTH_SHORT).show();
}

}
});

}
});


and
getExistingUserInfo
Method

public void getExistingUserInfo(){

FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference().child("allUsers").orderByChild("emailAddress")
.equalTo(mEmailStr)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
uid = childSnapshot.getKey();
Log.v("HimaAbosalem",uid);

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

}
});
database.getReference("allUsers").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//get user data from dataSnapshot
User=(HashMap<String,String>)dataSnapshot.getValue();
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


I need to use
HashMap
to send the data throw intent, but i gives me that
java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at this part of
getExistingUserInfo
method

database.getReference("allUsers").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//get user data from dataSnapshot
User=(HashMap<String,String>)dataSnapshot.getValue();
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});


i thought the problem was uid value equal null but i made a log and it's not, and i tried to send that uid throw an intent to another activity and i recieved it to found it's not null but when i tried to get the data in that new Activity it gives the same Error!!!

Answer

I solved this problem by divide the getExistingUserInfo to two separate Function and call the function that get allUsers at onDataChange to guarantee that user's id returns that's code after Edited:

public void getExistingUserInfo(){

FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference().child("allUsers").orderByChild("emailAddress")
        .equalTo(mEmailStr)
        .addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                    uid = childSnapshot.getKey();
                    Log.v("HimaAbosalem",uid);

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

    }
});
} 
 public void getUser(String uid){
  database.getReference("allUsers").child(uid).addListenerForSingleValueEvent(new  ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        //get user data from dataSnapshot
        User=(HashMap<String,String>)dataSnapshot.getValue();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
}
Comments