简体   繁体   English

尝试在空对象引用上调用虚拟方法“…………”

[英]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.

相关问题 尝试在 android studio firebase mainactivity 中的空对象引用上调用虚方法 boolean java.lang.String.equals(java.lang.Object) - Attempt to invoke virtual method boolean java.lang.String.equals(java.lang.Object) on a null object reference in android studio firebase mainactivity android firebase crashlytics 无法在 null ZA8CFDE6331BD59EB2AC966F8911Z4B6 上调用方法 all() - android firebase crashlytics Cannot invoke method all() on null object Android studio null object 参考错误 - Android studio null object reference error AWS Lambda - 调用另一个 lambda 函数的方法 - AWS Lambda - Invoke a method of another lambda function 所需参考处不存在 object - No object exists at the desired reference 在 null 上调用了方法“toDate”。 接收方:null 尝试调用:toDate() - The method 'toDate' was called on null. Receiver: null Tried calling: toDate() 谷歌一键登录 null 参考 - Google One Tap Sign In null reference 返回 null 的 dynamodb 扫描方法 - dynamodb scan method returning null Flutter 方法'[]'不能被无条件调用,因为接收者可以是'null' - Flutter The method '[]' can't be unconditionally invoked because the receiver can be 'null' 获取数据错误:未处理的异常:NoSuchMethodError:在 null 上调用了方法“[]” - fetcing data error :Unhandled Exception: NoSuchMethodError: The method '[]' was called on null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM