title: List null ouside the for loop
body: <pre><code> I need to use the existing elements in the firebase database. I took them over through CollectionReference.
  </code></pre>
  <p>If I want to use doctorsList outside of the loop, it returns null. How can I fix this? How can I call the list to show me all the values added by doctorsList.add ()?</p>
  <pre><code>   FirebaseFirestore db=FirebaseFirestore.getInstance();
      ArrayList&lt;String&gt; doctorsList=new ArrayList&lt;&gt;();
  
  @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_graph);
       CollectionReference collectionReference=db.collection(&quot;users&quot;);
  
          collectionReference.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
              @Override
              public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
                  if (task.isSuccessful()) {
  
                      List&lt;DocumentSnapshot&gt; myListOfDocuments = task.getResult().getDocuments();
                      for(DocumentSnapshot documentSnapshot:myListOfDocuments) {
                          CollectionReference collectionReference1 = documentSnapshot.getReference().collection(&quot;Appointment&quot;);
                          collectionReference1.get().addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
                              @Override
                              public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
                                  List&lt;DocumentSnapshot&gt; listDocuments = task.getResult().getDocuments();
                                  for (DocumentSnapshot documentSnapshot1 : listDocuments)
                                      doctorsList.add(documentSnapshot1.getString(&quot;doctorName&quot;));
  //                                  Toast.makeText(getApplicationContext(),doctorName,Toast.LENGTH_LONG).show();
  //HERE DISPLAY ALL ITEMS IN THE LIST
                              }
                          });
  
                      }
                     Toast.makeText(getApplicationContext(),doctorsList.toString(),Toast.LENGTH_LONG).show();
  // DOCTORSLIST IS EMPTY HERE
  
                  }
              }
          });
  
  
  
      }
  </code></pre>