无法从Firestore收集数据到Android [重复]

9gm1akwq  于 2024-01-04  发布在  Android
关注(0)|答案(1)|浏览(142)

此问题在此处已有答案

What is a NullPointerException, and how do I fix it?(12个回答)
17小时前关闭
我成功地将数据发送到Firestore(collection:Food),但我无法从Firestore(collection:Food)收集数据。我甚至添加了try/catch并设置了断点,但除了以下错误之外,我无法获得更多信息。
错误:
方法引发“java.lang.NullPointerException”异常。无法计算com.google.firebase.firestore.FirebaseFirestoreSettings.toString()
代码(从Firestore收集数据)错误:

  1. // MainActivity.java
  2. FirebaseFirestore db = FirebaseFirestore.getInstance();
  3. db.collection("Food")
  4. .get()
  5. .addOnCompleteListener(task -> {
  6. if (task.isSuccessful()) {
  7. ArrayList<CloudFood> arrayfood = new ArrayList<>();
  8. for (QueryDocumentSnapshot document : task.getResult()) {
  9. String id = document.getId();
  10. String foodName = document.getString("food_name");
  11. int per = document.getLong("per").intValue();
  12. double calorie = document.getDouble("calorie");
  13. double carbon = document.getDouble("carbon");
  14. double protein = document.getDouble("protein");
  15. double fat = document.getDouble("fat");
  16. CloudFood foodItem = new CloudFood(id, foodName, per, calorie, carbon, protein, fat);
  17. arrayfood.add(foodItem);
  18. }
  19. } else
  20. {
  21. Log.e(TAG, "Error getting documents: ", task.getException());
  22. }
  23. });

字符串

CloudFood.java

  1. package com.example.dietmanagement;
  2. public class CloudFood {
  3. private String id;
  4. private String foodName;
  5. private int per;
  6. private double calorie;
  7. private double carbon;
  8. private double protein;
  9. private double fat;
  10. // Constructor
  11. public CloudFood(String id, String foodName, int per, double calorie, double carbon, double protein, double fat) {
  12. this.id = id;
  13. this.foodName = foodName;
  14. this.per = per;
  15. this.calorie = calorie;
  16. this.carbon = carbon;
  17. this.protein = protein;
  18. this.fat = fat;
  19. }
  20. // Getters and setters
  21. public String getId() {
  22. return id;
  23. }
  24. public void setId(String id) {
  25. this.id = id;
  26. }
  27. public String getFoodName() {
  28. return foodName;
  29. }
  30. public void setFoodName(String foodName) {
  31. this.foodName = foodName;
  32. }
  33. public int getPer() {
  34. return per;
  35. }
  36. public void setPer(int per) {
  37. this.per = per;
  38. }
  39. public double getCalorie() {
  40. return calorie;
  41. }
  42. public void setCalorie(double calorie) {
  43. this.calorie = calorie;
  44. }
  45. public double getCarbon() {
  46. return carbon;
  47. }
  48. public void setCarbon(double carbon) {
  49. this.carbon = carbon;
  50. }
  51. public double getProtein() {
  52. return protein;
  53. }
  54. public void setProtein(double protein) {
  55. this.protein = protein;
  56. }
  57. public double getFat() {
  58. return fat;
  59. }
  60. public void setFat(double fat) {
  61. this.fat = fat;
  62. }
  63. }


代码(发送数据到Firestore)工作成功:

  1. FirebaseFirestore db = FirebaseFirestore.getInstance();
  2. Map<String, Object> db_user = new HashMap<String, Object>();
  3. db_user.put("food_name", food);
  4. db_user.put("per", per);
  5. db_user.put("calorie", cal);
  6. db_user.put("carbon", car);
  7. db_user.put("protein", pro);
  8. db_user.put("fat", fat);
  9. db.collection("Food").document(food + "_"+adInfo.getId()).set(db_user).
  10. addOnSuccessListener(new OnSuccessListener<Void>() {
  11. @Override
  12. public void onSuccess(Void aVoid) {
  13. Log.d(TAG, "DocumentSnapshot successfully written!");
  14. }
  15. })
  16. .addOnFailureListener(new OnFailureListener() {
  17. @Override
  18. public void onFailure(@NonNull Exception e) {
  19. Log.w(TAG, "Error writing document", e);
  20. }
  21. });

Firestore,collection:Food

x1c 0d1x的数据


jhkqcmku

jhkqcmku1#

我有两个问题。

1.我把字符串类型的数字包含在“per”,“calorie”等中,因为我贴在了问题上。

而且我在下面的代码中发现了一个错误。

  1. int per = document.getLong("per").intValue();
  2. double calorie = document.getDouble("calorie");
  3. double carbon = document.getDouble("carbon");
  4. double protein = document.getDouble("protein");
  5. double fat = document.getDouble("fat");
  6. CloudFood foodItem = new CloudFood(id, foodName, per, calorie, carbon, protein, fat);

字符串
我换了

  1. String per = document.getString("per");
  2. String calorie = document.getString("calorie");
  3. String carbon = document.getString("carbon");
  4. String protein = document.getString("protein");
  5. String fat = document.getString("fat");
  6. CloudFood foodItem = new CloudFood(id, foodName, Integer.parseInt(per), Double.parseDouble(calorie),
  7. Double.parseDouble(carbon), Double.parseDouble(protein), Double.parseDouble(fat));


第二个问题是太个人化了,但另一个文档的“per”,“calorie”是字符串类型的文本,所以我有一个错误,当我试图从字符串转换为整数或双精度。因此,我修复了数据的字符串类型的数字。

展开查看全部

相关问题