Home > Software engineering >  ArrayList add() method returns empty or blank firebase java
ArrayList add() method returns empty or blank firebase java

Time:06-15

I am facing a strange situation. Believe me. New to using Firestore database. I have a collection as follow,

List<String> myStringList = new ArrayList<String>(); 
ApiFuture<QuerySnapshot> apiFutureResults = query.get();
QuerySnapshot querySnapshot = apiFutureResults.get();
List<QueryDocumentSnapshot> documents = querySnapshot.getDocuments();
for(QueryDocumentSnapshot document: documents)
{
     log.debug(document.getString("myColumnName")); //prints value successfully
     String myString = document.getString("myColumnName"); // Same value assigned to String type.
     log.debug("Value of myString", myString);  //prints value successfully
     log.debug("Result is ::",myStringList.add(myString)); // prints empty or blank.
     log.debug("Arraylist size::", myStringList.size()); // Returns empty or blank.
     log.debug("Arraylist content::", myStringList); // Returns empty or blank  }

I was expecting true after addition. In my case I am getting myStringList.add(myString); returns empty. String is fetched from QueryDocumentSnapshot (com.google.cloud.firestore). In What scenarios ArrayList.add() method returns empty. Or Is there any issue with this approach to fetch records from Firestore. This is not android application. Just a Java product.

CodePudding user response:

Can you provide code by which you are instantiating log variable. I suspect problem may be in the way you are logging the variables. Generally, format to log is (format, list of arguments), but code snippet you have, expects output to be concatenated by ,

CodePudding user response:

Your code is printing the value returned by myStringList.add(). I suspect that's not what you intended. Printing the result of add() doesn't seem very useful for debugging purposes.

Maybe what you wanted to do is first add the item to the list first, then print the contents of the list.

myStringList.add(myString);
log.debug("Result is ::",myStringList); // print the list

You probably also want to print the list after the loop completes, not every iteration.

  • Related