将Firestore数据提取到自定义对象中 [英] Fetching firestore data into a custom object
问题描述
说onSucces方法下的Toast可以正常工作,但是onSucces之外的Toast不能正常工作
Toast under onSucces method is working but Toast outside onSucces is not working is says
"java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String com.example.collegecommune.User.getName()'"
"java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.collegecommune.User.getName()' on a null object reference"
我正在尝试从Firestore中获取当前用户数据,并将其存储在自定义类User中.我想将 documentSnapshot.toObject(User.class)存储在 currentUser 中.
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();
}
我最近尝试了以下代码:它也不起作用.
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);
}
推荐答案
通过查看当前的实现,我可以说-您的 onSuccess
调用是异步的&提取数据需要时间&更新 currentUser
对象.到那时,您在 onSuccess
块之外的吐司将执行&尝试从当时的 null 对象的 currentUser
对象中获取数据.因此,它将引发 nullPointetException
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
这篇关于将Firestore数据提取到自定义对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!