firestore中的数组对象返回null,而其他字段不是

时间:2020-06-16 17:02:12

标签: java android arrays google-cloud-firestore

我试图将文档的数据提取到我的自定义类对象中,并将数组值显示到recyclerview中。 我遇到的问题是,除了'products'数组对象返回空值之外,所有获取的文档的其他字段都已获取。

enter image description here

我的自定义对象类:

    private String IDliv;
    private String IDpan;
    private ArrayList<Object> products;

    public generalObject() {
    }

    public String getIDliv() {
        return IDliv;
    }

    public void setIDliv(String IDliv) {
        this.IDliv = IDliv;
    }

    public String getIDpan() {
        return IDpan;
    }

    public void setIDpan(String IDpan) {
        this.IDpan = IDpan;
    }

    public ArrayList<Object> getProds() {
        return products;
    }

    public void setProds(ArrayList<Object> prods) {
        this.products = prods;
    }

    public generalObject(String IDliv, String IDpan, ArrayList<Object> prods) {
        this.IDliv = IDliv;
        this.IDpan = IDpan;
        this.products = prods;
    }
}

还有我用来获取数据的代码:

        @Override
        public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
            assert documentSnapshots != null;
            for (DocumentChange document : documentSnapshots.getDocumentChanges()) {
            generalObject obj=document.getDocument().toObject(generalObject.class);
                Toast.makeText(getApplicationContext(),obj.getIDliv().toString(),Toast.LENGTH_LONG).show();
            if(obj.getProds()!=null){
            for(int i=0;i<obj.getProds().size();i++){
                String[] body;
                  body=obj.getProds().get(i).toString().split("\\+");
                ProductModel p = new ProductModel(body[1].toString(), body[2].toString(), body[3].toString());
                prod.add(p);

            }

            }else{
                Toast.makeText(getApplicationContext(),"stuff not clicking",Toast.LENGTH_LONG).show();
            }
            }
    }
    });
        adapter= new ProductAdapter(prod);
        RecyclerView recview= findViewById(R.id.items_recycler_view);
        recview.setHasFixedSize(true);
        recview.setLayoutManager(new LinearLayoutManager(this));
        recview.setAdapter(adapter);

我的错误日志

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference
        at com.example.showproducts.MainActivity$1.onEvent(MainActivity.java:56)
        at com.example.showproducts.MainActivity$1.onEvent(MainActivity.java:49)
        at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2(com.google.firebase:firebase-firestore@@21.4.3:1045)
        at com.google.firebase.firestore.Query$$Lambda$3.onEvent(Unknown Source:6)
        at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0(com.google.firebase:firebase-firestore@@21.4.3:42)
        at com.google.firebase.firestore.core.AsyncEventListener$$Lambda$1.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:101)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7529)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

关于如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

您的getter和方法getProds()setProds()与“产品”字段的名称不匹配。您可以重命名方法getProducts()setProducts(),也可以重命名字段“ prods”,使它们匹配。