title: How to avoid NullPointerException when deleting a document in Firestore?
body: <p>I want to give the user in my app the possibility to delete his account so when he clicks on the delete button a document gets deleted which contains all his informations. The name of the document is his displayName so I get this as a string but when I run the code you are seeing below I get a NullpointerException in this line:
   <code>String currentUsername = user.getDisplayName();</code>
  even though the displayName is not null.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I found the solution on my own, see the answer below.</p>
  
  <p>Here is my method:</p>
  
  <pre><code>
  btn_delete_account.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  
                  user.delete()
                          .addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {
                              @Override
                              public void onComplete(@NonNull Task&lt;Void&gt; task) {
                                  if (task.isSuccessful()) {
                                      deleteDocument();
                                  }
                              }
                          });
              }
          });
  
  ...
  
  public void deleteDocument (){
  
          FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  
          String currentUsername = user.getDisplayName();
  
          db.collection("User").document(currentUsername)
                  .delete()
                  .addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
                      @Override
                      public void onSuccess(Void aVoid) {
                          Log.d(TAG, "DocumentSnapshot successfully deleted!");
                          Toast.makeText(PersonalSettings.this, "Your account was successfully deleted.", Toast.LENGTH_SHORT).show();
                          Intent i = new Intent(PersonalSettings.this, SignInActivity.class);
                          startActivity(i);
                          finish();
                      }
                  })
                  .addOnFailureListener(new OnFailureListener() {
                      @Override
                      public void onFailure(@NonNull Exception e) {
                          Log.w(TAG, "Error deleting document", e);
                      }
                  });
      }
  </code></pre>