0

My profileActivity wont start due to getting a NullPointer exception, I assume the problem is with

FirebaseUser user = firebaseAuth.getCurrentUser();
textViewUserEmail.setText("Welcome " + user.getEmail());`

and that email is Null but I cant understand why. Here is my code:

import com.google.firebase.auth.FirebaseAuth;

inside onCreate:

textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);

firebaseAuth = FirebaseAuth.getInstance();

if (firebaseAuth.getCurrentUser() == null) {
    finish();
    startActivity(new Intent(this, Home.class));
}

FirebaseUser user = firebaseAuth.getCurrentUser();
textViewUserEmail.setText("Welcome " + user.getEmail());

Here is the log error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liamthedeveloper.foodiez/com.liamthedeveloper.foodiez.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getEmail()' on a null object reference
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
                                                   at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:154)
                                                   at android.app.ActivityThread.main(ActivityThread.java:6682)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getEmail()' on a null object reference
                                                   at com.liamthedeveloper.foodiez.ProfileActivity.onCreate(ProfileActivity.java:66)
                                                   at android.app.Activity.performCreate(Activity.java:6942)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) 
                                                   at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:154) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:6682) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
liam
  • 15
  • 1
  • 6

3 Answers3

0
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getEmail()' on a null object reference

It is clearly that the object of FirebaseUser is null,it means that the user in your code is null, not the email is null.

杜咸鱼
  • 19
  • 5
0
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();

String name = user.getDisplayName();

Then add this check to avoid NullPointerException

if(!TextUtils.isEmpty(name))
    textViewUserEmail.setText("Welcome " + user.getEmail());
Akshay Katariya
  • 1,464
  • 9
  • 20
0

You should rethink your null check of the Firebase user object.

if (firebaseAuth.getCurrentUser() == null) {
    finish();
    startActivity(new Intent(this, Home.class));
}

This will not stop the execution of onCreate, hence you will invoke a method on a null reference.

Make sure to stop the execution in your null check, i.e :

if (firebaseAuth.getCurrentUser() == null) {
    finish();
    startActivity(new Intent(this, Home.class));
    return;
}

This may be even more clear.

if(objectToCheck != null){
 // objectToCheck can be used
}else{
 // Handle the case when objectToCheck is null 
}
rckrd
  • 3,124
  • 1
  • 13
  • 23