Home > front end >  Why is my document reference returning a null string although there is no error
Why is my document reference returning a null string although there is no error

Time:10-28

Here is my database structure:

enter image description here Here is my full code `

public class Homepage extends AppCompatActivity {
    private View measure , history;
    private Button signout;
    private TextView txtwelcome;
    FirebaseAuth fAuth;
    FirebaseFirestore Fstore;
    String userID;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homepage);
        measure = findViewById(R.id.measurecard);
        history = findViewById(R.id.historycard);
        signout = findViewById(R.id.btnsginout);
        txtwelcome = findViewById(R.id.txtwelcome);
        fAuth = FirebaseAuth.getInstance();
        Fstore = FirebaseFirestore.getInstance();
        userID = fAuth.getUid();
        DocumentReference documentReference = Fstore.collection("users").document(userID);
        documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                 txtwelcome.setText("Welcome " documentSnapshot.getString("Name"));
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(Homepage.this,e.toString(),Toast.LENGTH_LONG).show();
            }
        });



        measure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(),measure.class));
            }
        });
        history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(),history.class));

            }
        });
        signout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(),MainActivity.class));
            }
        });
    }


` it just puts on the edit text welcome null it doesn't print any exception I've been trying for a while please help. Although when the user signs up it stores the data in the data base but I can't seem to fetch it. I tried a snapshotlistner and successlistner on completelistner nothing worked , I excpected to see welcome hamza on my text view

CodePudding user response:

You're getting null because the UID that is generated when the user is authenticated it's different than the document ID that exists in your database. Why do I say that? Because the length of the UID that comes from the authentication mechanism is 24 while the length of your document ID (RRUr...xhIB) is only 20. So most likely when you have added the users to Firestore, you have used the CollectionReference#add() method. That being said, your reference points to a document that doesn't exist, hence that null.

To set the UID as an ID of the document, you have to pass that UID to CollectionReference#document() method like this:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("users").document(uid).set(userObject);
//                                         
  • Related