Reputation:
I am using firebase to do the authentication in my application, and everything works fine, but when i log in i want to display the user email that is logged in, and what i did crashes the application when i run it, so i did this:
auth = FirebaseAuth.getInstance();
userTxt.setText(auth.getCurrentUser().getEmail().toString());
i defined a auth object and then used that auth to get the current user email, after that all in one line i passed that to my textView, what am i doing wrong here?
StackTrace
04-09 09:00:11.247 2987-2987/com.esmad.pdm.friendlymanager E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.esmad.pdm.friendlymanager, PID: 2987
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.esmad.pdm.friendlymanager/com.esmad.pdm.friendlymanager.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference
at com.esmad.pdm.friendlymanager.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
Upvotes: 2
Views: 2441
Reputation: 3974
Your logcat is giving you the answer:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference
so your invoking getCurrentUser()
on a null object.
extra:
I have just come upon another situation where the user's email would return null even if you have correctly initialized your FirebaseUser
There's a closed bug report already filed with google:
When you go to the Auth > Sign-In Methods page of your project in the Firebase console. do you have One account per email address on or off? If you allow multiple accounts per email address, you will get null for FirebaseUser.getEmail()
You have to use the option: "Prevent creation of multiple accounts with the same email address" or else FirebaseUser.getEmail() will return null
Ps: only users who first logged in after the option was turned off will be able to use this method successfully
Upvotes: 1
Reputation: 1627
it crash because getCurrentUser()
might return null
try this
mAuth = FirebaseAuth.getInstance();
final FirebaseUser theUser = mAuth.getCurrentUser();
if (theUser !=null)
String _UID = theUser.getEmail().toString();
and I prefer to use AuthStateListener
In MainActivity
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
then in onCreate
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
String _UID = Theuser.getUid();
String Uemail= Theuser..getEmail().toString();
} else {
// User is signed out
}
}
};
and in onStart
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
and in onStop
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
Upvotes: 0