title: Querying Firestore for uid document after authentification => IllegalStateException
body: <p>I am using both Firebase Authentification and Firestore for my Android app. What I am trying to do is the following:</p>
  
  <ul>
  <li>the user signs in</li>
  <li>if it's the first time the user signs in a document named by his uid is created</li>
  <li>if the user has already signed in before (hence document named by uid already exists) then I load some further data.</li>
  </ul>
  
  <p>Here's my logic to solve this:</p>
  
  <ul>
  <li>get the FirebaseUser from FirebaseAuth instance</li>
  <li>from the FirebaseUser I get the uid</li>
  <li>build a DocumentReference with this uid</li>
  <li>use get() query on the DocumentReference</li>
  <li>if DocumentSnapshot is != null then user already exists in firestore</li>
  <li>if DocumentSnapshot == null the user doesn't exist and I create it in firestore</li>
  </ul>
  
  <p>I was testing the code below:</p>
  
  <pre><code>    FirebaseUser user = mAuth.getCurrentUser();
      if(user != null) {
          // get uid from user
          String uid = user.getUid();
  
          // make a query to firestore db for uid
          DocumentReference userDoc = db.collection("users").document(uid);
          userDoc.get().addOnCompleteListener(new OnCompleteListener&lt;DocumentSnapshot&gt;() {
              @Override
              public void onComplete(@NonNull Task&lt;DocumentSnapshot&gt; task) {
                  if (task.isSuccessful()) {
                      DocumentSnapshot document = task.getResult();
                      if (document != null) {
                          Log.d(LOG_TAG, "DocumentSnapshot data: " + task.getResult().getData());
                      } else {
                          Log.d(LOG_TAG, "No such document");
                      }
                  } else {
                      Log.d(LOG_TAG, "get failed with ", task.getException());
                  }
              }
          });
      }
  </code></pre>
  
  <p>When uid exists in firestore I get the log message with appropriate data but when it doesn't I get the following exception and I can't find a way to get to use DocumentSnapshot.exists():</p>
  
  <pre><code>java.lang.IllegalStateException: This document doesn't exist. Use DocumentSnapshot.exists() to check whether the document exists before accessing its fields.
  </code></pre>
  
  <p>Can anyone help me understand what I am doing wrong ?</p>
  
  <p>Thanks a million ! :)</p>