title: Attempt to invoke virtual method 'java.lang.String ' on a null object reference
body: <p>I created a Firebase Firestore and I insert some data in a collection Users through a sign up page. All is working fine, but when I fetch the data back in a Profile Activity, first I make a new variable mUser of type User ( a model I created), but if I use that variable mUser outside the "for" where I get the documents from database, it's null.</p>
  
  <p>Can you please look over and tell me how can I fetch correctly, please? Thank you in advance!</p>
  
  <pre><code>public class ProfileActivity extends AppCompatActivity {
  
      //Firebase
      FirebaseAuth mAuth;
      FirebaseFirestore db;
      StorageReference mStorageRef;
      StorageTask mUploadTask;
  
      //Layout
  
      ImageView profilePic;
      TextView fullnameView;
      TextView rateView;
      TextView addressView;
  
      //Vars
  
      User mUser;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_profile);
          BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);
          Menu menu = bottomNavigationView.getMenu();
          MenuItem menuItem = menu.getItem(1);
          menuItem.setChecked(true);
          bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
              @Override
              public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                  switch (menuItem.getItemId())
                  {
                      case R.id.nav_home:
                          Intent h = new Intent(ProfileActivity.this, MainActivity.class);
                          startActivity(h);
                          overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
                          finish();
  
                          break;
                      case R.id.nav_profile:
  
                              Intent p = new Intent(ProfileActivity.this, ProfileActivity.class);
                              startActivity(p);
                              overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
                              finish();
  
  
                          break;
                      case R.id.nav_settings:
                          Intent s = new Intent(ProfileActivity.this, SettingsActivity.class);
                          startActivity(s);
                          overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
                          finish();
  
                          break;
                  }
                  return false;
              }
          });
  
  
          mAuth = FirebaseAuth.getInstance();
          db = FirebaseFirestore.getInstance();
          mStorageRef = FirebaseStorage.getInstance().getReference("users-images");
  
          fullnameView = findViewById(R.id.fullnameview);
          addressView = findViewById(R.id.addressview);
          profilePic = findViewById(R.id.profilePicView);
          rateView = findViewById(R.id.rate);
  
  
          FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
          if(user != null)
          {
              db.collection("Users").whereEqualTo("email", user.getEmail())
                      .get()
                      .addOnCompleteListener(new OnCompleteListener&lt;QuerySnapshot&gt;() {
                          @Override
                          public void onComplete(@NonNull Task&lt;QuerySnapshot&gt; task) {
                              if(task.isSuccessful())
                              {
                                  for(QueryDocumentSnapshot documentSnapshot: task.getResult()) {
  
                                             String fullname = documentSnapshot.get("fullname").toString();
                                             String address = documentSnapshot.get("address").toString();
                                             String city =  documentSnapshot.get("city").toString();
                                             String state =  documentSnapshot.get("state").toString();
                                             String country = documentSnapshot.get("country").toString();
                                             String phone = documentSnapshot.get("phone").toString();
                                             String zipcode = documentSnapshot.get("zipcode").toString();
                                             String type = documentSnapshot.get("type").toString();
                                             String rate = documentSnapshot.get("rate").toString();
                                             Boolean isActivated = documentSnapshot.getBoolean("isActivated");
                                             List&lt;Card&gt; cards= (List&lt;Card&gt;) documentSnapshot.get("cards");
                                             List&lt;Comments&gt; comments = (List&lt;Comments&gt;) documentSnapshot.get("comments");
                                             List&lt;Documents&gt; documents = (List&lt;Documents&gt;) documentSnapshot.get("documents");
                                             String profilePic = documentSnapshot.get("profilePic").toString();
                                             String email = documentSnapshot.get("email").toString();
                                             String password = documentSnapshot.get("password").toString();
  
  
                                             mUser = new User(
                                                     fullname,
                                                     address,
                                                     city,
                                                     state,
                                                     country,
                                                     phone,
                                                     zipcode,
                                                     type,
                                                     rate,
                                                     isActivated,
                                                     cards,
                                                     comments,
                                                     documents,
                                                     profilePic,
                                                     email,
                                                     password
  
                                             );
  
  
                                  }
                              }
                          }
                      });
          }
  
  
  
              fullnameView.setText(mUser.getFullname());
              addressView.setText(mUser.getAddress());
  
  
  
  
      }
  }
  
  
  </code></pre>
  
  <p>After this I get </p>
  
  <pre><code> Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.****.Models.User.getFullname()' on a null object reference
          at com.example.***.ProfileActivity.onCreate(ProfileActivity.java:161)
  </code></pre>
  
  <p>because I'm using the mUser outside this for </p>
  
  <pre><code>for(QueryDocumentSnapshot documentSnapshot: task.getResult()) {
  ...
  }
  </code></pre>
  
  <p>How can I fetch the data and use it outside that "for"?</p>
  
  <p>Thank you!</p>