You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firestore空指针异常求助:调用getLong时Android应用崩溃

解决Firestore空指针异常:Attempt to invoke virtual method 'getLong' on null object reference

你遇到的空指针异常,核心是直接调用了null对象的getLong()方法,具体出在两个关键位置:

  • 从docList获取ID为jml_kuis的文档时,该文档可能不存在,导致homeListDoc为null,后续调用homeListDoc.getLong("count")触发崩溃
  • 循环中根据kuisX_id取文档时,对应的文档ID可能不在当前查询结果里,导致homeDoc为null,调用homeDoc.getLong("no_kuis")触发崩溃

修复后的代码

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);
                    }

                    // 先检查jml_kuis文档是否存在
                    QueryDocumentSnapshot homeListDoc = docList.get("jml_kuis");
                    if (homeListDoc == null) {
                        completeListener.OnFailure();
                        return;
                    }

                    // 检查count字段是否存在且有效
                    Long homeCount = homeListDoc.getLong("count");
                    if (homeCount == null || homeCount <= 0) {
                        completeListener.OnSuccess(); // 无数据也算加载完成
                        return;
                    }

                    for( int i=1; i <= homeCount; i++){
                        String docID = homeListDoc.getString("kuis" + String.valueOf(i) + "_id");
                        // 跳过无效的文档ID
                        if (docID == null || docID.isEmpty()) {
                            continue;
                        }
                        QueryDocumentSnapshot homeDoc = docList.get(docID);
                        // 跳过不存在的文档
                        if (homeDoc == null) {
                            continue;
                        }

                        // 检查必填字段是否存在
                        Long noKuisLong = homeDoc.getLong("no_kuis");
                        String homeName = homeDoc.getString("name");
                        if (noKuisLong == null || homeName == null) {
                            continue;
                        }

                        int numOfTest = noKuisLong.intValue();
                        g_homeList.add(new HomeViewModel(docID, homeName, numOfTest));
                    }

                    completeListener.OnSuccess();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    completeListener.OnFailure();
                }
            });
}

关键修复说明

  1. 文档存在性检查:给homeListDoc和homeDoc加null判断,避免调用null对象的方法
  2. 字段有效性检查:确认count、no_kuis、name等字段存在且符合预期类型
  3. 容错处理:遇到无效数据时直接跳过,而不是崩溃,保证应用稳定性

新手注意事项

  • 永远不要假设Firestore的文档或字段一定存在,必须做null检查
  • 可以登录Firebase控制台,确认kuis集合下确实有jml_kuis文档,且包含count和对应的kuisX_id字段
  • 每个kuisX_id对应的文档必须存在于kuis集合中,且包含no_kuis和name字段

内容的提问来源于stack exchange,提问作者Siti Mutia Suryani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 09:00:39