1

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) 
  • 3
    The [logcat will tell you the reason your app has stopped](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). Please [edit] your question with it. – OneCricketeer Apr 09 '17 at 08:45
  • 1
    I see two opportunities for NullPointer here. 1) No current user 2) No email – OneCricketeer Apr 09 '17 at 08:46
  • Make sure you have downloaded `google-services.json` file from [firebase console](https://console.firebase.google.com/) – Shreyash S Sarnayak Apr 09 '17 at 09:16
  • i did, just a question, do i need to place it inside the app folder or outside? –  Apr 09 '17 at 09:25

2 Answers2

1

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()

enter image description here

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

HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
0

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);
    }
}
Shreyash S Sarnayak
  • 2,309
  • 19
  • 23
Elsunhoty
  • 1,609
  • 14
  • 27
  • can you explain me better why you need that 2 lines at onStart and onStop, i don't get it why there is a nullpointerexception, if i just open that activity if i have a user, else i open a registration, if i remove my 2 lines it enters the homepage, that means it has a user :S, i checked that before –  Apr 09 '17 at 09:04
  • a little late but i can answer to that, you just use mAuth.addAuthStateListener(mAuthListener); in the onStart because when the MainActivity or your Activity where you are checking if the user exists to do something, so, when the app just starts it will check if the user is != null, so it will tell to you if there is an user logged in, and will start listening whenever the user is != null or == to null. as onStop goes it says that if the user is still loggedd in it will remove the listener that is listening about the user state. – Gastón Saillén Jan 29 '18 at 02:34