0

im trying to get a table in firestore to store additional details about a user authenticated from firebase auth using one time authentication. the authentication bit works really fine,problem is when i try to check if a user exists in firestore, i get a null value instead.

check out my code below`

public FirebaseFirestore db=FirebaseFirestore.getInstance();
FirebaseUser mUser;
public String userId;
public final String[] useRole=new String[1];
private Boolean fstoreUser;
private Boolean found;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    checkSignIn();
    checkFirestoreuser();
    getUseRole();
    TextView mText=(TextView) findViewById(R.id.tv_testuser);

    switch (useRole[0]){
        case "LC":
            mText.setText("im an LC");
            break;
        case "Ordinary":
            mText.setText("im an ordionary user");
    }
}

private void checkFirestoreuser() {
        found = findUserInFirestore(userId);
        if(!found)
            createUserInFirestore();

}

private Boolean findUserInFirestore(String userId) {
    final Boolean[] foundOrNot = new Boolean[1];
    DocumentReference docRef=db.collection("users").document(userId);
    docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot doc=task.getResult();
                        foundOrNot[0] =doc.exists();

                    }else{
                        Log.d("failed",task.getException().getMessage());
                        Toast.makeText(MainActivity.this, task.getException().getMessage(), 
                            Toast.LENGTH_LONG).show();
                    }
                }

            }
            );
    return foundOrNot[0];
}


private void checkSignIn(){
    FirebaseAuth auth=FirebaseAuth.getInstance();
    if(auth.getCurrentUser()!=null) {
        mUser=auth.getCurrentUser();
        userId=mUser.getUid();

    }
}



private void getUseRole() {

    if (fstoreUser||found) {
        db.document("users/"+userId).get()
                .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()){
                            DocumentSnapshot doc=task.getResult();
                            useRole[0]=doc.getString("role");
                        }else{
                            Toast.makeText(MainActivity.this, task.getException().getMessage(), 
                                Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    } else {
        createUserInFirestore();
    }
}

private void createUserInFirestore() {

    Map<String,Object> newUser= new HashMap<>();
    newUser.put("role","Ordinary");

    db.collection("users").document(userId).set(newUser)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (!task.isSuccessful()){

                        Toast.makeText(MainActivity.this,"Please Try Again: "+ 
                            task.getException().getMessage(), Toast.LENGTH_LONG).show();

                    }
                }
            });
    checkSignIn();
}

inside the findUserInFirestore() method, it just doest check so foundOrNot returns with a null value, and this throws anull pointer exception. please help me double check what went wrong, this has been beating me up and over for two nights now.

  • You cannot simply use `useRole[0]` outside the `onComplete` method. Please check the duplicate to see why do you have this behavior and how can you solve this using a custom callback. – Alex Mamo Apr 02 '20 at 12:04
  • thanks for helping @AlexMamo , but actually, useRole is a final global one element string array which will carry the value returned from `getUseRole()` method after a call to firestore for the document.....actually the code doesnt even run that far, it crashes at the checkFirestoreUser() method when a null value from findUserInFireStore() is returned to the boolean found...i think no document is fetched from firestore and i dont know why.....could double check the code in the findUserInFirestore() method – right_click Apr 02 '20 at 23:23
  • i also manually put a user in my firestore users collection with the user id as the document id but findUserInFirestore() still failed,thats why i think its just not reading or writting – right_click Apr 02 '20 at 23:45
  • 1
    actually, i realised where the main problem was, after i successfully wrote to the database, the main problem was on the asynchronous behaviour of firebase, what i needed was a callback. – right_click Apr 13 '20 at 21:55

0 Answers0