我正在尝试从Fragment类的Firebase中的数据库中获取数据 我需要从firebase数据库中使用DataSnapshot获取名称和电子邮件(nombre,correo)。我使用userID = currentUser.getUid()来查找数据关联,但我有一个错误java.lang.NullPointerException:尝试调用虚方法
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”模型
public class UserInformation {
private String nombre;
private String correo;
public UserInformation(){
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getCorreo() {
return correo;
}
public void setCorreo(String correo) {
this.correo = correo;
}
}
[Firebase database][1]
答案 0 :(得分:0)
看起来这条线路是个问题吗?
userInformation.setNombre(ds.child(userID).getValue(UserInformation.class).getNombre());
ds可能为null,请尝试log ds value并查看它是否为null