Firestore无法将文档转换为对象

问题描述 投票:0回答:1

我正在尝试检索文档,但是我无法使其正常工作。这是我的代码:

fm.getColRefProgramms().document(programmKey).collection("items").document("HIXQobZtlMxn4OblgOMi").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    String s = documentSnapshot.getId();
                    OverviewItem item = documentSnapshot.toObject(OverviewItem.class);
                    item.setKey(item.getKey());
                }
            });

String返回正确的ID,因此可以从数据库中找到并加载文档,但无法从中创建OverviewIdem对象。

我收到此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.reflect.Constructor.newInstance(java.lang.Object[])' on a null object reference

这是OverviewItem类:

public class OverviewItem implements IProgrammItem, Serializable{

private String instance;
private String key;
private Programm programm;
private ArrayList<BasicElement> elements;

public OverviewItem() {}

public void setInstance(String instance) {
    this.instance = instance;
}

public String getInstance() {
    return instance;
}

@Exclude
public Programm getProgramm() {
    return programm;
}

@Exclude
public String getKey() {
    return key;
}

@Exclude
public void setKey(String key) {
    this.key = key;
}

@Exclude
public void setProgramm(Programm programm) {
    this.programm = programm;
}

public ArrayList<BasicElement> getElements() {
    return elements;
}

public void setElements(ArrayList<BasicElement> elements) {
    this.elements = elements;
}
}

这是我的文档:

Database

java android google-cloud-firestore
1个回答
0
投票

FIX:我等待了半个小时,然后开始检索数据(已检索到Date的值,并且不再为null)。也许数据库需要花费一些时间来更新,所以请给它一些时间(如果您添加了新值或文档),然后再次检查。

我面临着同样的问题。这是我的代码:

        firebasefirestore.collection("events").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {

            for(DocumentChange doc: documentSnapshots.getDocumentChanges()){

                if(doc.getType() == DocumentChange.Type.ADDED){

                    Map<String, Object> a = doc.getDocument().getData();
                    HomeViewModel event = doc.getDocument().toObject(HomeViewModel.class);
                    eventlist.add(event);
                    eventRecyclerAdapter.notifyDataSetChanged();

                }
            }
        }
    });

这里我创建了一个变量'a'来查看数据,并且已成功从Firestore检索数据(我正在调试器中检查)。

Value of a

但是当我使用toObject()时,Date为null。

我的HomeViewModel类:

public class HomeViewModel{

    private String Date;
    private String Description;
    private String Imagepath;
    private String Title;

    public void setDate(String date) {
        this.Date = date;
    }

    public String getDate() {
        return Date;
    }

    public String getImagepath() {
        return Imagepath;
    }

    public void setImagepath(String imagepath) {
        Imagepath = imagepath;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public String getDescription() {
        return Description;
    }

    public String getTitle() {
        return Title;
    }

    public HomeViewModel(){
    }

    public HomeViewModel(String title, String desc, String date, String imagepath) {
        this.Title = title;
        this.Description = desc;
        this.Date = date;
        this.Imagepath = imagepath;
    }

}

因此很明显,toObject是问题所在。

© www.soinside.com 2019 - 2024. All rights reserved.