-2

I am trying to get the database collection from Firestore but getting two errors first warning is:

Do not place Android context classes in static fields

Second, when I call the database, it shows:

java.lang.NullPointerException: Attempt to invoke virtual method

I found so many similar questions, but it does not help me so I asked new question

public static List<BookModel> bookList = new ArrayList<>();
    public static FirebaseFirestore g_firestore;
    public static void  LoadBook(MyCompleteListener completeListener){
        bookList.clear();
        g_firestore.collection("BOOKS").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 bookListDoc = docList.get("Categories");


                    assert bookListDoc != null;
                    Long bookCount = bookListDoc.getLong("COUNT");
                    for (int i=1; 1<= bookCount ;i++){
                        String bookID =  bookListDoc.getString("BOOK"+String.valueOf(i)+"_ID");
                        QueryDocumentSnapshot bookDoc = docList.get(bookID);
                        assert bookDoc != null;
                        int noOfBook = Objects.requireNonNull(bookDoc.getLong("NO_OF_BOOK")).intValue();
                        String bookName = bookDoc.getString("NAME");
                        String bookType = bookDoc.getString("TYPE");

                        bookList.add(new BookModel( bookID,bookName,bookType,noOfBook));

                    }
                    completeListener.onSuccess();

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

                }
            });
    }
}

java.lang.RuntimeException: Unable to start activity
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)' on a null object reference` at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)

2
  • Can you please include the stack trace? Commented Feb 25, 2024 at 8:23
  • ` java.lang.RuntimeException: Unable to start activity java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)' on a null object reference` at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782) Commented Feb 25, 2024 at 11:01

1 Answer 1

0

You are getting the this error:

java.lang.RuntimeException: Unable to start activity java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)' on a null object reference

Because of the following line of code:

g_firestore.collection("BOOKS").get().addOnSuccessListener(/* ... /*)

This means that g_firestore is null. Why? Because you didn't initialize it in the first place. To solve this, please change this line:

public static FirebaseFirestore g_firestore;

To:

public static FirebaseFirestore g_firestore = FirebaseFirestore.getInstance();
Sign up to request clarification or add additional context in comments.

5 Comments

but i getting this java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Long com.google.firebase.firestore.QueryDocumentSnapshot.getLong(java.lang.String)' on a null object reference
Looks like docList.get("Categories") returns null - Why should it not do that?
@SarthakTiwari That seems to be another NPE. So the fact that you are getting another error, means that the initial error disappeared and this is good news :) but this sounds like a new issue which basically should be considered another question. So, please post a new question, here on StackOverflow, using its own MCVE, so I and other Firebase developers can help you.
Did my answer help? Can I help you with other information regarding the initial issue?
yes it help but i am getting still issue that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.