Reputation: 55
Toast under onSucces method is working but Toast outside onSucces is not working is says
"java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.collegecommune.User.getName()' on a null object reference"
I am trying to fetch current user data from the firestore and store it in custom class User. I wanted to store documentSnapshot.toObject(User.class) in currentUser.
static FirebaseAuth auth = FirebaseAuth.getInstance();
static FirebaseUser cUser = auth.getCurrentUser();
public static User currentUser;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fetchProfile();
// currentUser = new FirebaseInteract(this).fetchProfile(cUser);
setupToolbar();
navItemSelected();
// role = currentUser.getRole();
// Toast.makeText(this, currentUser.getName() + "", Toast.LENGTH_SHORT).show();
if (savedInstanceState == null) {
navigationView.setCheckedItem(R.id.home);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new HomeFragment()).commit();
}
}
public void fetchProfile() {
db.collection("users").document(cUser.getEmail()).get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
currentUser = documentSnapshot.toObject(User.class);
Toast.makeText(MainActivity.this, currentUser.getName() + "", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, currentUser.getSemester() + "", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "Successfully fetched user profile", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, e.getMessage() + "", Toast.LENGTH_SHORT).show();
}
});
Toast.makeText(MainActivity.this, currentUser.getName() + "22", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, currentUser.getSemester() + "22", Toast.LENGTH_SHORT).show();
}
I have recently tried this code: it also does'nt work.
public void fetchProfile(String email) {
final ProgressDialog dialog = new ProgressDialog(activity);
dialog.setMessage("Fetching your profile...");
dialog.show();
db.collection("users").document(email).get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
User user1 = documentSnapshot.toObject(User.class);
Toast.makeText(activity, user1.getEmail() + " 00", Toast.LENGTH_SHORT).show();
MainActivity.setCurrentUser(user1);
dialog.dismiss();
}
}).addOnFailureListener(onFailureListener);
}
Upvotes: 0
Views: 93
Reputation: 2504
By looking at current implementation I can say - your onSuccess
call is asynchronous & it'll take time to fetch data & update currentUser
object .
Till that time your toast which is outside the onSuccess
block will execute & try to fetch data from currentUser
object which will be null at that point of time.. therefore it throws nullPointetException
Upvotes: 2