In my Firestore database, I have a 'categories' collection with multiple documents which in turn have 'products' sub-collections with respective documents.
I'm trying to get all documents in all sub-collections and populate into a Recylerview but I have failed.
Here is the code I have;
private void populateAllProducts() {
Log.d(TAG, "populateAllProducts called: ");
database
.collection(Globals.CATEGORIES)
.get()
.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
Category category = document.toObject(Category.class);
Log.d(TAG, "Category: " + category.getName());
database
.collection(Globals.CATEGORIES)
.document(category.getUuid())
.collection(Globals.PRODUCTS)
.get()
.addOnCompleteListener(task1 -> {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot snapshot : Objects.requireNonNull(task1.getResult())) {
Product product = snapshot.toObject(Product.class);
Log.d(TAG, "All products: " + product.getName());
}
}
})
.addOnFailureListener(e -> Log.e(TAG, "Error: ", e));
}
}
})
.addOnFailureListener(e -> {
vars.verityApp.crashlytics.log("Error while fetching categories");
Log.e(TAG, "Error occured: ", e);
vars.verityApp.crashlytics.recordException(e);
});
}
But I receive this Fatal Exception;
2020-08-11 16:27:16.358 26161-26161/com.verityfoods E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.verityfoods, PID: 26161
java.lang.NullPointerException: Provided document path must not be null.
at com.google.firebase.firestore.util.Preconditions.checkNotNull(Preconditions.java:147)
at com.google.firebase.firestore.CollectionReference.document(CollectionReference.java:103)
at com.verityfoods.ui.bottomviews.shop.ShopFragment.lambda$populateCategories$2$ShopFragment(ShopFragment.java:87)
at com.verityfoods.ui.bottomviews.shop.-$$Lambda$ShopFragment$dIuQdpUfodkE_zdMSo4L2Rr4DL0.onComplete(Unknown Source:2)
at com.google.android.gms.tasks.zzj.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I will appreciate your help!