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

9gm1akwq  于 11个月前  发布在  Android
关注(0)|答案(1)|浏览(117)

此问题在此处已有答案

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收集数据)错误:

// 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;
    }
}


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

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

x1c 0d1x的数据


jhkqcmku

jhkqcmku1#

我有两个问题。

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

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

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);

字符串
我换了

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));


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

相关问题