Firestore无法将Document转换为Object

时间:2018-04-19 19:53:03

标签: java android google-cloud-firestore

我试图检索一个文档,但我无法让它工作。这是我的代码:

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,因此从数据库中找到并加载了Document,但它无法从中创建一个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

1 个答案:

答案 0 :(得分: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是问题所在。