0

I have an array with my object and Cloud Firestore. In Firestore I have 11 documents with two fields each. I take data from the cloud and set the values of the two fields in an Object that I created called Food.

Then I want to read the value of the name field of each element in the array but Android give me NullPointerException because I'm calling the method getName() on a null object. Before I also tried using an HashMap instead of an Array but the result is the same, call to the elements give me the same exception.

Here is the last version of my code :

    Food[] foodList = new Food[11];


    cloudDatabase.collection("food").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {

                Food food;
                int counter = 0;
                for(QueryDocumentSnapshot foodDocument : task.getResult()) {
                    if (counter<12) {
                        String foodName = foodDocument.get("name").toString();
                        float foodPrice = Float.valueOf(foodDocument.get("price").toString());

                        food = new Food(foodName, foodPrice);
                        foodList[counter] = food;

                        food = null;

                        counter++;
                    }
                    else {
                        Log.w("Food Counter", "Limit reached!");
                    }
                }


        }
    });

    for (Food foodElement : foodList) {
        String name = foodElement.getName(); //here I get the NullPointerException
        Log.d("Food Names", name);
    }

I searched many things but I still didn't found the soluction. What do you suggest to do? Can it be a problem of the CompleteListener of Firestore? Many Thanks!

Gabriel
  • 464
  • 4
  • 17
  • 1
    Hi:) call "for (Food foodElement : foodList)" inside the "cloudDatabase.collection("food").get().addOnCompleteListener" will solve it(add it after the "for" loop end scope) . its because retrieving data from firestore thread didn't finish when "for (Food foodElement : foodList)" start to run – Ben Feb 07 '20 at 21:45
  • So Android can retrieve data from Firestore and continue with the code in the same time? Is there a way to stop the flow of the code until the firestore thread didn't finish? – Gabriel Feb 07 '20 at 21:47
  • 1
    When you try to retrieve data from firestore you use listener that call onComplete when it finish to download ALL information from firebase BUT the app continue to the next line(aka your "for" with Foodlist) when FoodList is still empty coz onComplete didn't run yet. and when it done downloading onComplate is trigged and start to fill the list. – Ben Feb 07 '20 at 21:52
  • 2
    Yes. In fact that is pretty much how most cloud-based APIs work these days: the call is made while your app continues to run (so your user is not blocked). Then when the data is available, your callback (here `onComplete`) is called with the data. This means that all code that needs the data from the database must be inside `onComplete` or be called from there. For a longer explanation, see https://stackoverflow.com/questions/51000169/how-to-check-a-certain-data-already-exists-in-firestore-or-not/51002413#51002413 – Frank van Puffelen Feb 07 '20 at 21:53
  • Is a good way to create something like a method and call it after the data have been downloaded? – Gabriel Feb 07 '20 at 21:54
  • Thank you so much, I was trying to understand it for like two hours! :D – Gabriel Feb 07 '20 at 21:59

0 Answers0