Android端无法从Firestore的Food集合读取数据问题求助
问题:Firestore写入成功但读取失败,抛出NullPointerException
成功向Firestore的Food集合写入数据,但无法从该集合读取数据。添加try/catch并设置断点后,仅能获取到如下错误:
错误:
Method threw 'java.lang.NullPointerException' exception. Cannot evaluate com.google.firebase.firestore.FirebaseFirestoreSettings.toString()
读取数据的出错代码(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; } }
成功写入数据的代码
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的Food集合数据截图


内容的提问来源于stack exchange,提问作者yuki yuki
相关产品推荐
相关产品推荐

