0

I successfully sent data to Firestore (collection:Food), but I couldn't collect data from Firestore (collection:Food). I even added try/catch and set breakpoints, but I couldn't gain more information other than the following error.

Error:

Method threw 'java.lang.NullPointerException' exception. Cannot evaluate com.google.firebase.firestore.FirebaseFirestoreSettings.toString()

Code (collect data from Firestore) error:

// MainActivity.java 
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("Food")
    .get()
    .addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            ArrayList<CloudFood> arrayfood = new ArrayList<>();

            for (QueryDocumentSnapshot document : task.getResult()) {
                String id = document.getId();
                String foodName = document.getString("food_name");
                int per = document.getLong("per").intValue();
                double calorie = document.getDouble("calorie");
                double carbon = document.getDouble("carbon");
                double protein = document.getDouble("protein");
                double fat = document.getDouble("fat");

                CloudFood foodItem = new CloudFood(id, foodName, per, calorie, carbon, protein, fat);
                arrayfood.add(foodItem);
            }
        } else
        {
            Log.e(TAG, "Error getting documents: ", task.getException());
        }
    });

CloudFood.java

package com.example.dietmanagement;

public class CloudFood {
    private String id;
    private String foodName;
    private int per;
    private double calorie;
    private double carbon;
    private double protein;
    private double fat;

    // Constructor
    public CloudFood(String id, String foodName, int per, double calorie, double carbon, double protein, double fat) {
        this.id = id;
        this.foodName = foodName;
        this.per = per;
        this.calorie = calorie;
        this.carbon = carbon;
        this.protein = protein;
        this.fat = fat;
    }

    // Getters and setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFoodName() {
        return foodName;
    }

    public void setFoodName(String foodName) {
        this.foodName = foodName;
    }

    public int getPer() {
        return per;
    }

    public void setPer(int per) {
        this.per = per;
    }

    public double getCalorie() {
        return calorie;
    }

    public void setCalorie(double calorie) {
        this.calorie = calorie;
    }

    public double getCarbon() {
        return carbon;
    }

    public void setCarbon(double carbon) {
        this.carbon = carbon;
    }

    public double getProtein() {
        return protein;
    }

    public void setProtein(double protein) {
        this.protein = protein;
    }

    public double getFat() {
        return fat;
    }

    public void setFat(double fat) {
        this.fat = fat;
    }
}

Code(send data to Firestore) works successfully:

FirebaseFirestore db = FirebaseFirestore.getInstance();
    Map<String, Object> db_user = new HashMap<String, Object>();
    db_user.put("food_name", food);
    db_user.put("per", per);
    db_user.put("calorie", cal);
    db_user.put("carbon", car);
    db_user.put("protein", pro);
    db_user.put("fat", fat);
    db.collection("Food").document(food + "_"+adInfo.getId()).set(db_user).
            addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.d(TAG, "DocumentSnapshot successfully written!");
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.w(TAG, "Error writing document", e);
                }
            });

Firestore, collection:Food

Firestore

enter image description here

5
  • Can you provide the stack trace so we know which line(s) of your code are leading to the exception? Commented Jan 3, 2024 at 3:52
  • According to the Firebase Console screenshot above, you have 2 documents in Food with IDs 3 and food. Do both documents have a food_name value? Commented Jan 3, 2024 at 3:54
  • Does the arrayfood contain any data by the end of the loop? Please respond using @AlexMamo Commented Jan 3, 2024 at 7:35
  • @Greg Fenton Appreciate the comments. Yes, both documents have food_name value. I was so stupid that I didn't put number data into "per", "calorie" of the document' ID = "food". Commented Jan 3, 2024 at 12:53
  • @AlexMamo Appreciate your comment. No, arrayfood didn't contain any data. I found that I had a problem with "int per = document.getLong("per").intValue();" and app crashed. Commented Jan 3, 2024 at 12:55

1 Answer 1

0

I had 2 issues.

1. I contained String type of numbers into "per", "calorie",etc as I pasted at the question.

And I found an error in the following codes.

int per = document.getLong("per").intValue();
double calorie = document.getDouble("calorie");
double carbon = document.getDouble("carbon");
double protein = document.getDouble("protein");
double fat = document.getDouble("fat");
CloudFood foodItem = new CloudFood(id, foodName, per, calorie, carbon, protein, fat);

I changed to

String per = document.getString("per");
String calorie = document.getString("calorie");
String carbon = document.getString("carbon");
String protein = document.getString("protein");
String fat = document.getString("fat");
CloudFood foodItem = new CloudFood(id, foodName, Integer.parseInt(per), Double.parseDouble(calorie),
Double.parseDouble(carbon), Double.parseDouble(protein), Double.parseDouble(fat));

Second issue was too personal, but the another document's "per", "calorie" were String type of texts, so I had an error when I tried to convert from String to Integer or Double. Therefore, I fixed data to String type of numbers.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.