首页 > 解决方案 > 如何在 Firestore 文档中获取嵌套对象?

问题描述

我的文档设计如下:

Document[IgaSZRSo8viqK9VxpJ6H]:
name:David
type:1
proList:
    item1:
        name:apple
        price:1
    item2:
        name:grape
        price:1
    item3:
        name:orange
        price:2
amount:
    basic:10
    extra:20

上传到firestore时一切顺利,但是当我从firestore下载日期时,当我输入以下内容时,我总是收到异常“java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法”: proList.item1.name

我可以得到所有信息,包括amount.basicamount.extra没有任何错误!

我不知道为什么,也无法在官方文档中找到任何信息。

编辑

以下是Java中的结构:

任务实体.java:

public class TaskEntity{
    public String name, type;
    public proListEntity;
    public amountEntity;

    public TaskEntity(){

    }
    public void set(TaskEntity entity){
    this.name=entity.name;
    this.type=entity.type;
    this.proListEntity=entity.proListEntity;
    this.amountEntity=entity.amountEntity;
    }
}

proListEntity.java

public class proListEntity{
    public String name;
    public long price;


    public proListEntity(){

    }
    public void set(proListEntity entity){
    this.name=entity.name;
    this.price=entity.price;
    }
}

从 Firestore 下载数据:

TaskEntity mEntity = documentSnapshot.toObject(TaskEntity.class);

标签: javaandroidfirebase

解决方案


推荐阅读