java.lang.NullPointerException:尝试调用虚拟方法+;火基
我试图从Firebase中的一个数据库中获取片段类中的数据 我需要使用DataSnapshot从firebase数据库获取姓名和电子邮件(nombre,correo)。我使用userID=currentUser.getUid()查找数据关联,但我有一个错误java.lang.NullPointerException:尝试调用虚拟方法java.lang.NullPointerException:尝试调用虚拟方法+;火基,java,android,android-fragments,Java,Android,Android Fragments,我试图从Firebase中的一个数据库中获取片段类中的数据 我需要使用DataSnapshot从firebase数据库获取姓名和电子邮件(nombre,correo)。我使用userID=currentUser.getUid()查找数据关联,但我有一个错误java.lang.NullPointerException:尝试调用虚拟方法 public class ProfileFragment extends Fragment { private FirebaseAuth mAuth;
public class ProfileFragment extends Fragment {
private FirebaseAuth mAuth;
private FirebaseDatabase database;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
private DatabaseReference myNewRef;
//Data Firebase
private String userID;
private String email;
public ProfileFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myRef = FirebaseDatabase.getInstance().getReference();
myNewRef = myRef.child("Usuarios");
mAuth = FirebaseAuth.getInstance();
database = FirebaseDatabase.getInstance();//Usuarios siempre con mayusculas
final FirebaseUser currentUser = mAuth.getCurrentUser();
userID = currentUser.getUid();
email = currentUser.getEmail();
DatabaseReference reference = database.getReference();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser currentUser1 = firebaseAuth.getCurrentUser();
if(currentUser != null){
Log.d(TAG, "signed in. " + currentUser.getUid());
}else{
Log.d(TAG, "Signed out");
}
}
};
myNewRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_profile, container, false);
showToolbar("",false,view);
RecyclerView cardsRecycler = (RecyclerView) view.findViewById(R.id.pictureProfileRecycler);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
cardsRecycler.setLayoutManager(linearLayoutManager);
CardAdapterRecyclerView cardAdapterRecyclerView = new CardAdapterRecyclerView(buildCard(),R.layout.cardview_category,getActivity());
cardsRecycler.setAdapter(cardAdapterRecyclerView);
return view;
}
public void showData(DataSnapshot dataSnapshot){
for(DataSnapshot ds: dataSnapshot.getChildren()){
UserInformation userInformation = new UserInformation();
Log.d(TAG, "USER ID " + userID);
userInformation.setNombre(ds.child(userID).getValue(UserInformation.class).getNombre());
userInformation.setCorreo(ds.child(userID).getValue(UserInformation.class).getCorreo());
Log.d(TAG, "name " + userInformation.getNombre());
Log.d(TAG, "email " + userInformation.getCorreo());
}
}
@Override
public void onStart(){
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop(){
super.onStop();
if(mAuthListener != null){
mAuth.removeAuthStateListener(mAuthListener);
}
}
public void showToolbar(String tittle, boolean upButton, View view){
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(tittle);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(upButton);
}
}
我有这个结果
FATAL EXCEPTION: main
Process: biz.kapta.desafioautomotriz, PID: 2815
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String biz.kapta.desafioautomotriz.model.UserInformation.getNombre()' on a null object reference
我已经有了“用户信息”模型
看起来这条线有问题,对吗
userInformation.setNombre(ds.child(userID).getValue(UserInformation.class).getNombre());
ds可能为空,请尝试记录ds值,看看它是否为空我打印了ds.child(userID),是的,它为空,但是,如果我只打印ds,则显示示例,值={nombre:kevin,correo:k@gmail.com},这些是我需要的数据,现在的问题是,如何获得第一个字段(nombre)?第二个呢(科雷奥)?或者如何在文本视图中设置。。。。谢谢
userInformation.setNombre(ds.child(userID).getValue(UserInformation.class).getNombre());