Firebase .getUID NullPointerException when authen

后端 未结 3 1682
甜味超标
甜味超标 2021-02-10 06:21

I will add data to database when I register, but I got

Java.lang.NullPointerException: Attempt to invoke virtual method \'java.lang.String com.google.firebase.auth.

相关标签:
3条回答
  • 2021-02-10 06:34
     FirebaseUser user = auth.getCurrentUser();
     userID = user.getUid();
    

    To run above two lines, you must (I'm stressing you must) have username/password based authenticated in the Firebase. It is throwing NullPointerException because most of the novice programmer forget to setup user based authentication in Firebase console.

    You can add the user in Firebase Authentication.

    If you are using Firebase Realtime Database then I suggest you add following rules in RULES tab.

    // These rules grant access to a node matching the authenticated
    // user's ID from the Firebase auth token
    {
      "rules": {
        "users": {
          "$user_id": {
            ".read": "$user_id === auth.uid",
            ".write": "$user_id === auth.uid"
          }
        }
      }
    

    }

    0 讨论(0)
  • 2021-02-10 06:36

    What is the type of auth you have mentioned in the code, Replace it with single line and check if it works

            String mUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    

    You may also check the documentation for full working code example: https://firebase.google.com/docs/auth/android/manage-users

    0 讨论(0)
  • 2021-02-10 06:53

    Check this:

    if(auth.getCurrentUser() != null){
        uid = auth.getCurrentUser().getUID();
    }
    
    0 讨论(0)
提交回复
热议问题