Reputation: 589
my app shut down after login, I don't know why?
this is a part of the code from the profile page, after the user login it should go to this page :
public class profilepage extends AppCompatActivity {
TextView inputFullname;
TextView uniemail;
FirebaseAuth mAuth;
FirebaseUser mUser;
DatabaseReference mRefUser, RootRef, mUserOldRef;
String userEmail, userID, userName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profilepage);
mRefUser = FirebaseDatabase.getInstance().getReference().child("Profile");
mUserOldRef = FirebaseDatabase.getInstance().getReference().child("users");
RootRef = FirebaseDatabase.getInstance().getReference();
mAuth = FirebaseAuth.getInstance();
mUser = mAuth.getCurrentUser();
inputFullname = findViewById(R.id.profileName);
uniemail = findViewById(R.id.profileEmail);
LoadUserProfile();
private void LoadUserProfile() {
mUserOldRef.child(mUser.getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
userName = snapshot.child("userName").getValue().toString();
inputFullname.setText(userName);
userEmail = snapshot.child("userEmail").getValue().toString();
uniemail.setText(userEmail);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
the error is :
Process: com.example.ksuersapp, PID: 10342
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.ksuersapp.profilepage$6.onDataChange(profilepage.java:200)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
any idea what I can do to solve the problem? the mean idea of the code is to retrieve the information about the user from the firebase Realtime database
Upvotes: 0
Views: 60
Reputation: 600100
It looks like no user information exists at the location you're trying to read. To handle this situation in your code, check whether the snapshot exists:
mUserOldRef.child(mUser.getUid()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
userName = snapshot.child("userName").getValue().toString();
inputFullname.setText(userName);
userEmail = snapshot.child("userEmail").getValue().toString();
uniemail.setText(userEmail);
}
else {
Log.i("Database", "No user info exists for "+mUser.getUid());
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException(); // never ignore errors
}
});
Upvotes: 1