gpt4 book ai didi

java - 将 firestore 数据获取到自定义对象中

转载 作者:行者123 更新时间:2023-12-02 01:19:36 25 4
gpt4 key购买 nike

onSucces 方法下的 Toast 正常工作,但 onSucces 之外的 Toast 不起作用

“java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.String com.example.collegecommune.User.getName()””

我正在尝试从 firestore 获取当前用户数据并将其存储在自定义类 User 中。我想将 documentSnapshot.toObject(User.class) 存储在 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 block 之外的 toast 将执行并尝试从 currentUser 对象中获取数据,此时该对象将为 null time..因此它抛出 nullPointetException

关于java - 将 firestore 数据获取到自定义对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57949704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com