I'm trying to get authenticated user id to pass it as argument for child(userID) method to be reference node in firebase database but it's give a null pointer exception.
as it's supposed to do, i logged in first to get the id of current user
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
firebaseAuth = FirebaseAuth.getInstance();
firebaseSignIn();
Log.i("userID", "user id is: " + userID);
databaseReference = FirebaseDatabase.getInstance().getReference().child("users available");
userReference = databaseReference.child(userID); //error line
}
public void firebaseSignIn() {
String email = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("email", "");
String password = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).getString("password", "");
Log.i(getClass().getName(), "email is: " + email + "and password is: " + password);
if(email.equals("") || password.equals(""))
startActivity(new Intent(MapsActivity.this, WelcomeActivity.class));
else {
firebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
Log.i("userID", "user id is: " + userID);
Toast.makeText(MapsActivity.this, R.string.signed_in, Toast.LENGTH_LONG).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MapsActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
this is the error i got.
Caused by: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at com.google.firebase.database.DatabaseReference.child(com.google.firebase:firebase-database@@17.0.0:101)
at com.example.findnurse.MapsActivity.onCreate(MapsActivity.java:116)
at android.app.Activity.performCreate(Activity.java:7340)
at android.app.Activity.performCreate(Activity.java:7331)