Search code examples
androidfirebaseandroid-recyclerviewnullpointerexceptionadapter

Android app crashes at runtime after successful gradle build


I'm new to android and firestore. I have built a small app and whenever I run my app, the app crashes at runtime eventhough the gradle build is successful. The following is the code for the main activity and Logcat for the better understanding.

package com.example.make;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.TextView;

import com.google.firebase.firestore.DocumentChange;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;

import io.opencensus.tags.Tag;

public class MainActivity extends AppCompatActivity {

private static final String Tag = "Firelog";

private List<Users> usersList;
private UsersListAdapter usersListAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RecyclerView mMainList = findViewById(R.id.main_list);

    usersList  = new ArrayList<>();
    usersListAdapter = new UsersListAdapter(usersList);
    mMainList.setLayoutManager(new LinearLayoutManager(MainActivity.this));
    mMainList.setAdapter(usersListAdapter);


    TextView mName = findViewById(R.id.item_name);
    TextView mStatus = findViewById(R.id.item_status);

    try {

        FirebaseFirestore db = FirebaseFirestore.getInstance();

        db.collection("Users").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

                if (e != null) {

                    Log.d(Tag, "Error : " + e.getMessage(), new NullPointerException());


                    for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {

                        if (doc.getType() == DocumentChange.Type.ADDED) {

                            doc.getDocument().getReference().collection("Users").addSnapshotListener(new EventListener<QuerySnapshot>() {
                                @Override
                                public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                                    if (e != null) {
                                        Log.d("", "Error :" + e.getMessage(), new NullPointerException());
                                    }

                                    for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {

                                        if (doc.getType() == DocumentChange.Type.ADDED) {

                                            Log.d("USer Name :", doc.getDocument().getId());
                                        }
                                    }
                                }
                            });

                            Users users = doc.getDocument().toObject(Users.class);
                            usersList.add(users);
                            usersListAdapter.notifyDataSetChanged();

                        }

                    }
                }
            }
        });
    } catch (NullPointerException error) {
        mName.setText(error.getMessage());
    }
}

}

This is the error I get,

12-03 13:59:30.576 26036-26036/com.example.make E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.make, PID: 26036 java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()' on a null object reference at com.example.make.MainActivity$1.onEvent(MainActivity.java:65) at com.example.make.MainActivity$1.onEvent(MainActivity.java:56) at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2(com.google.firebase:firebase-firestore@@17.1.3:885) at com.google.firebase.firestore.Query$$Lambda$3.onEvent(com.google.firebase:firebase-firestore@@17.1.3) at com.google.firebase.firestore.util.ExecutorEventListener.lambda$onEvent$0(com.google.firebase:firebase-firestore@@17.1.3:42) at com.google.firebase.firestore.util.ExecutorEventListener$$Lambda$1.run(com.google.firebase:firebase-firestore@@17.1.3) at android.os.Handler.handleCallback(Handler.java:754) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6383) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

I also put the try catch block to catch the nullpointer exception but it didn't work.Any help is appreciated. Thanks.


Solution

  • The error states that :

    java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.google.firebase.firestore.QuerySnapshot.getDocumentChanges()' on a null object reference

    And inside your code:

    for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
    

    The queryDocumentSnapshots object is null (as can be inferred by the annotation).