-1

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!!!

  • 1
    http://ideone.com/PPHi95 ... obviously `getExistingUserInfo` do nothing but just invoking async methods and do not wait till they are finished ... – Selvin Sep 14 '16 at 15:30
  • 1
    See this answer to a similar question: http://stackoverflow.com/a/39472821/4815718 – Bob Snyder Sep 14 '16 at 15:36
  • how to do that "The way to deal with this behavior is to reframe the flow of your program from "first get the list of friends, then do xyz with it" to "whenever the list of friends changes, do xyz with it"." in my Case, i have the user already and i want to retrieve it's data! – Ibrahim Abousalem Sep 14 '16 at 15:54
  • 1
    So your reframing becomes: "when I get the user data, I want to do xyz with it" – Frank van Puffelen Sep 14 '16 at 17:59
  • how to do that because i need the data ready in the click listener at sign in or should i do second approach send that uid throw an intent to another activity and get the data ? – Ibrahim Abousalem Sep 14 '16 at 18:32

1 Answers1

1

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) {

    }
});
}
Ahmed El Sherif
  • 160
  • 1
  • 10