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


Foodwith IDs3andfood. Do both documents have afood_namevalue?arrayfoodcontain any data by the end of the loop? Please respond using @AlexMamo