0

The code below crashes for many users. For some users, this data is returning null. I have no idea how to solve this. Please I need help!

Regard

Error Code:

java.lang.String com.google.firebase.auth.q.a0()' on a null object reference

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

My code:

public String getUid() {
   String getuids = FirebaseAuth.getInstance().getCurrentUser().getUid();
    if (getuids != null){
        return FirebaseAuth.getInstance().getCurrentUser().getUid();
    }else{
        Toast.makeText(mContext, R.string.somethingerrors, Toast.LENGTH_SHORT).show();
        startActivity(new Intent(mContext, LoginActivity.class));
    }
    return null;
}
Mehmet Ata
  • 11
  • 3
  • Does this answer your question? [Avoiding NullPointerException in Java](https://stackoverflow.com/questions/271526/avoiding-nullpointerexception-in-java) – sanjeevRm Jun 13 '21 at 00:40
  • It would appear that `FirebaseAuth.getInstance().getCurrentUser()` is returning `null`. As the [javadoc](https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth#getCurrentUser()) says: *"Returns the currently signed-in FirebaseUser or `null` if there is none. Use `getCurrentUser() != null` to check if a user is signed in."* – Stephen C Jun 13 '21 at 01:50
  • And ... obviously ... if the user is not signed in, you won't be able to get their user UID. (There are various possible explanations as to why there is currently no signed in user. Review the answers to the questions returned by this Google search: "firebaseauth getcurrentuser stackoverflow".) – Stephen C Jun 13 '21 at 01:58

0 Answers0