1

When I tried to test to find a document that is not in my firestore, I don't know why it sends a nullPointerError. What is the cause of this error? This is my code to get the list of documents that I want. In this case, I use userID as some arbitrary string that is not in my firestore.

If I tried to test it with a document that is available in my firestore it works. I was expecting for an empty list when I use an arbitrary userID. In the test console it said that the NullPointerException is coming from line "return db.runTransaction(tx -> {". This question is different from null pointer description as I already tried if(budgetRef==null) return budgets but it didn't work. Thank you

return db.runTransaction(tx -> {
            List<Budget> budgets = new ArrayList<>();
            ApiFuture<QuerySnapshot> budgetRef = tx.get(db.collection("users").document(userID).collection("budgets").orderBy("start_date", Query.Direction.DESCENDING));
            for (QueryDocumentSnapshot budgetDocument : budgetRef.get().getDocuments()) {
                Budget budget = createBudgetByDocument(budgetDocument, tx);
                if(budget.getStart_date().compareTo(currentDate) <= 0 && budget.getEnd_date().compareTo(currentDate) >= 0){
                    budgets.add(budget);
                }
            }
            return budgets;
        }).get();
  • Sounds like `db` is null. You have some debugging to do to figure out why that is. We can't see from what you show here why you're expecting it to be different. – Doug Stevenson Aug 04 '20 at 04:22
  • No the db is not null I already test it when the userID is available inside the firestore. – Farhan Khalifa Aug 04 '20 at 04:24

0 Answers0