Firebase .getUID NullPointerException when authen

后端 未结 3 1681
甜味超标
甜味超标 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"
          }
        }
      }
    

    }

提交回复
热议问题