0

I am fetching data from firestore and storing it in Arraylist but even after initializing it, it return nullpointer exception

Code:

private FirebaseFirestore Fs = FirebaseFirestore.getInstance();
private usermodel User;
 HashSet<String> Schls;
 ArrayList<String> SchlArray;

 Schls= new HashSet<String>();
        Fs.collection("School").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if(task.isSuccessful()){
                    for(QueryDocumentSnapshot Doc : task.getResult()){
                        User = Doc.toObject(usermodel.class);
                        if(User.getType().equals("School")){
                            Log.d(TAG,User.getType()+User.getSchoolid());
                            Schls.add(User.getSchoolid()+"=>"+User.getName());
                        }
                    }
                }
                SchlArray = new ArrayList<>(Schls);
            }
        });

        for(String i : SchlArray){
            Log.d(TAG,i);
        }

Error Logs :

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference
        at com.example.firestooredemo.SelectorForSchlRes.onCreate

Updated Code :

Schls= new HashSet<String>();
        Fs.collection("School").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if(task.isSuccessful()) {
                    for (QueryDocumentSnapshot Doc : task.getResult()) {
                        User = Doc.toObject(usermodel.class);
                        if (User.getType().equals("School")) {
                            Log.d(TAG, User.getType() + User.getSchoolid());
                            Schls.add(User.getSchoolid() + "=>" + User.getName());
                        }
                    }
                    SchlArray = new ArrayList<>(Schls);
                    for (String i : SchlArray) {
                        Log.d(TAG, i);
                    }
                }
            }
        });
                Log.d(TAG,"Outside onComplete");
                SchlArray = new ArrayList<>(Schls);
                for(String i : SchlArray){
                Log.d(TAG,"Outside onComplete");
                    Log.d(TAG,i);
                }

No Error Logs but Debugging logs :

2021-09-26 01:30:36.464 21834-21834/com.example.firestooredemo D/Datacheck: Outside onComplete 2021-09-26 01:30:36.855 21834-21834/com.example.firestooredemo D/Datacheck: School459 2021-09-26 01:30:36.855 21834-21834/com.example.firestooredemo D/Datacheck: School479 2021-09-26 01:30:36.856 21834-21834/com.example.firestooredemo D/Datacheck: School461 2021-09-26 01:30:36.856 21834-21834/com.example.firestooredemo D/Datacheck: School464 2021-09-26 01:30:36.857 21834-21834/com.example.firestooredemo D/Datacheck: School471 2021-09-26 01:30:36.857 21834-21834/com.example.firestooredemo D/Datacheck: School455 2021-09-26 01:30:36.858 21834-21834/com.example.firestooredemo D/Datacheck: 464=>Samarth Vidyalaya 2021-09-26 01:30:36.858 21834-21834/com.example.firestooredemo D/Datacheck: 471=>Shiv Samarath School 2021-09-26 01:30:36.858 21834-21834/com.example.firestooredemo D/Datacheck: 461=>Ruia School 2021-09-26 01:30:36.858 21834-21834/com.example.firestooredemo D/Datacheck: 455=>S. T. Pradhan School 2021-09-26 01:30:36.858 21834-21834/com.example.firestooredemo D/Datacheck: 459=>K. J. Somaiya School 2021-09-26 01:30:36.858 21834-21834/com.example.firestooredemo D/Datacheck: 479=>NKTT School

No log call detected from for loop out of OnComplete

  • This is the expected behavior. Data is loaded from Firestore (and most modern cloud APIs) asynchronously, and while that is going on your main code (and thus your `for(String i : SchlArray){`) continues to execute. Then when the data is available, your `onComplete` gets called with it - and that then sets `SchlArray`. The solution is always the same: any code that needs the data from Firestore must be inside `onComplete` or be called from there. I'll link some existing questions, but also check https://stackoverflow.com/search?q=%5Bgoogle-cloud-firestore%5D%5Bandroid%5D+outside. – Frank van Puffelen Sep 25 '21 at 20:26

0 Answers0