[英]Attempt to invoke virtual method '.........' on a null object reference
i'm a newbie and i have a project for my thesis but i got some problems.我是新手,我有一个论文项目,但遇到了一些问题。 so, i always get this error when i run my project, because of that my app always force close.所以,当我运行我的项目时,我总是会遇到这个错误,因为我的应用程序总是强制关闭。
Attempt to invoke virtual method 'java.lang.Long com.google.firebase.firestore.QueryDocumentSnapshot.getLong(java.lang.String)' on a null object reference.尝试在空对象引用上调用虚拟方法“java.lang.Long com.google.firebase.firestore.QueryDocumentSnapshot.getLong(java.lang.String)”。
i don't know where's the problems, or maybe i write the wrong code.我不知道问题出在哪里,或者我写错了代码。 please help me!请帮我! thankyou!谢谢你!
public static void loadHome(MyCompleteListener completeListener){
g_homeList.clear();
g_firestore.collection("kuis").get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
Map<String, QueryDocumentSnapshot> docList = new ArrayMap<>();
for (QueryDocumentSnapshot doc : queryDocumentSnapshots){
docList.put(doc.getId(), doc);
}
QueryDocumentSnapshot homeListDoc = docList.get("jml_kuis");
long homeCount = homeListDoc.getLong("count");
for( int i=1; i <= homeCount; i++){
String docID = homeListDoc.getString("kuis" + String.valueOf(i) + "_id");
QueryDocumentSnapshot homeDoc = docList.get(docID);
int numOfTest = homeDoc.getLong("no_kuis").intValue();
String homeName = homeDoc.getString("name");
g_homeList.add(new HomeViewModel(docID, homeName, numOfTest));
}
completeListener.OnSuccess();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
completeListener.OnFailure();
}
});
}
Looks like some of your doc Ids are null.看起来您的某些文档 ID 为空。 To fix this you might to check the data coming from firebase for the missing id.要解决此问题,您可以检查来自 firebase 的数据是否缺少 id。
To make the code run have add null checks like this:要使代码运行,请像这样添加空检查:
for(QueryDocumentSnapshot doc : queryDocumentSnapshots){
if(doc.getId()!==null){
docList.put(doc.getId(), doc);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.