gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:15:15 26 4
gpt4 key购买 nike

我正在尝试将文档的数据提取到我的自定义类对象中,并将数组值显示到回收器 View 中。我遇到的问题是,除了返回 null 的“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)

知道如何解决这个问题吗?

最佳答案

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

关于java - firestore 中的数组对象返回 null,而其他字段则不返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62413836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com