【问题标题】:Attempt to invoke virtual method: a null object reference尝试调用虚方法:空对象引用
【发布时间】:2018-08-04 06:50:43
【问题描述】:

我收到Null Object Reference,当我退出时应用程序崩溃。

代码:

 //Get Comments Count
firebaseFirestore.collection("Posts/" + postId + "/Comments").addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

        if (!documentSnapshots.isEmpty())
        {
            int count = documentSnapshots.size();
            holder.updateCommentsCount(count);
        }
        else if(documentSnapshots.isEmpty())
        {
            holder.updateCommentsCount(0);
        }
    }
});

错误:

尝试在空对象引用上调用虚拟方法“boolean com.google.firebase.firestore.QuerySnapshot.isEmpty()”

【问题讨论】:

  • 请添加您的数据库结构。

标签: java android firebase google-cloud-firestore


【解决方案1】:

您的应用程序崩溃,因为您没有初始化 QuerySnapshot 并且它为空。

private QuerySnapshot querySnap;

在 onCreate 中初始化 QuerySnapshot

querysnap = new QuerySnapshot (this, " ");

编辑:

QuerySnapshotException 之前给出@Nullable 并在其之后给出错误回调。

public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
if (e != null) {
          //          Log.w(TAG, "this is the error", e);
                    return;
                }
        if (!documentSnapshots.isEmpty())
            {

检查 Frank 建议的答案。一旦您想停止收听更新,最好取消订阅。

【讨论】:

  • 先生,我正在使用 recyclerViewAdapter。错误在适配器类中。那么先生,我将在哪里放置 QuerySnapshot
  • 先生,我现在通过添加(活动)上下文解决了这个问题。代码:addSnapshotListener((Activity) context, new EventListener()
  • 但现在我面临另一个问题。现在我的帖子乱七八糟了。
【解决方案2】:

看起来传递给onEventQuerySnapshot 可以为空,您必须在代码中注意这一点:

public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
    if (documentSnapshots == null || documentSnapshots.isEmpty())
    {
        holder.updateCommentsCount(0);
    }
    else
    {
        int count = documentSnapshots.size();
        holder.updateCommentsCount(count);
    }
}

documentSnapshots 变为 null 的原因可能是您的应用在用户退出后无法访问此代码正在观察的特定数据。在注销用户之前,您可能需要先考虑 detaching all listeners

【讨论】:

  • 我为你完成了代码sn-p。请注意,我强烈建议您花一些时间学习如何解决和防止此类空指针异常,因为它们在您刚开始时非常常见。有关详细说明,请参阅stackoverflow.com/questions/218384/…
  • 先生,我现在通过添加(活动)上下文解决了这个问题。代码:addSnapshotListener((Activity) context, new EventListener()
  • 但现在我面临另一个问题。现在我的帖子乱七八糟了。
【解决方案3】:

试试这个代码,它工作正常
我也遇到同样的问题,就这样解决。

firebaseFirestore.collection("Posts/" + blogPostId + "/Likes").addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

        if(queryDocumentSnapshots != null){

            if(!queryDocumentSnapshots.isEmpty()){

                int count = queryDocumentSnapshots.size();

                holder.updateLikesCount(count);
            }
            else {
                holder.updateLikesCount(0);
            }


        }
    }
});

【讨论】:

    猜你喜欢
    • 2021-11-07
    • 1970-01-01
    • 2019-01-10
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多