-2

I am getting a Null Object Reference and the app crashes when I sign out.

Code:

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

Error:

Attempt to invoke virtual method 'boolean com.google.firebase.firestore.QuerySnapshot.isEmpty()' on a null object reference

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
M Zaid Hayat
  • 11
  • 10

3 Answers3

1

Your app crashes because you have not initalized QuerySnapshot and it is null.

private QuerySnapshot querySnap;

Initalize QuerySnapshot in the onCreate with

querysnap = new QuerySnapshot (this, " ");

EDIT:

Give the @Nullable before QuerySnapshot & Exception and an error callback after it.

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

OR check the answer suggested by Frank. It's a good practice to unsubscribe once you want to stop listening to updates.

sanjeev
  • 1,664
  • 19
  • 35
0

It looks like the QuerySnapshot passed into onEvent can be null, and you'll have to guard against that in your code:

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

The reason the documentSnapshots becomes null is likely that your app loses access to the specific data this code is observing once the user signs out. You may want to consider first detaching all listeners before signing the user out.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I completed the code snippet for you. Note that I **highly** recommend you spend some time learning how to troubleshoot and prevent null pointer exceptions like this, as they are incredibly common when you're just getting started. For a good explanation, see https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Aug 04 '18 at 21:30
  • Sir I have solved this issue now by adding (Activity) context. Code: addSnapshotListener((Activity) context, new EventListener() – M Zaid Hayat Aug 04 '18 at 21:37
  • But now I am facing the other issue. Now my posts are not in order. – M Zaid Hayat Aug 04 '18 at 22:12
0

try this code, it's work properly
I also face the same problem and solve this way.

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


        }
    }
});