I am trying to get the data from firestore database but i getting the error when i define that the value is not null but error is not remove my code

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



                    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);
                        int noOfBook = 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) {

                }
            });
}

I getting the error is

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 at com.sarthak.mathadda.DbQuery$4.onSuccess(DbQuery.java:70) at com.sarthak.mathadda.DbQuery$4.onSuccess(DbQuery.java:54) at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@18.0.2:1) at android.os.Handler.handleCallback(Handler.java:958) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:205) at android.os.Looper.loop(Looper.java:294)

my DbQuery 70 line is int noOfBook = bookDoc.getLong("NO_OF_BOOK").intValue(); is this and 54 line .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() { this when i remove the 71 line it will go for 72 line I have also try with **long on place Long ** but it not work how I can solve this please do not close with similar question

0

There are 0 best solutions below