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.
Can you please look over and tell me how can I fetch correctly, please? Thank you in advance!
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<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> 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<Card> cards= (List<Card>) documentSnapshot.get("cards");
List<Comments> comments = (List<Comments>) documentSnapshot.get("comments");
List<Documents> documents = (List<Documents>) 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());
}
}
After this I get
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)
because I'm using the mUser outside this for
for(QueryDocumentSnapshot documentSnapshot: task.getResult()) {
...
}
How can I fetch the data and use it outside that "for"?
Thank you!