Reputation: 89
My Firestore structure looks something like this:
-(coll)users
-(doc)uniqueID
name
email
(coll)clients
-(doc)uniqueID
clientName
clientEmail
What I am trying to achieve is the following:
I tried to make a query using the code below as per the documentation:
clientsCollection = db.collection(FIRESTORE_COLLECTION_USERS)
.document(mUid)
.collection(FIRESTORE_COLLECTION_CLIENTS);
clientsCollection
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (DocumentSnapshot document: task.getResult()){
Log.d(LOG_TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(LOG_TAG, "error getting documents: ", task.getException());
}
}
});
I get the following RuntimeException
:
java.lang.NullPointerException: Provided document path must not be null.
I get this even if the clients collection exits with some documents in it named by unique uid.
Thanks for any clue you could give me! :)
Upvotes: 3
Views: 3891
Reputation: 600110
The error message indicates that mUid
is null
when you run the first statement. Most likely this means that you run this code before the user is signed in.
Make sure that you only call this code after the user has signed in, e.g. from an AuthStateListener.onAuthStateChanged()
:
FirebaseAuth.getInstance().addAuthStateListener(new AuthStateListener() {
public void onAuthStateChanged(FirebaseAuth auth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
clientsCollection = db.collection(FIRESTORE_COLLECTION_USERS)
.document(user.getUid())
.collection(FIRESTORE_COLLECTION_CLIENTS);
clientsCollection
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (DocumentSnapshot document: task.getResult()){
Log.d(LOG_TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(LOG_TAG, "error getting documents: ", task.getException());
}
}
}
}
})
Upvotes: 2