Firebase .getUID NullPointerException when authen

我的梦境 提交于 2019-12-21 02:41:42

问题


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.FirebaseUser.getUid()' on a null object reference

I try something, but don't work

Here's My code

    auth = FirebaseAuth.getInstance();

    database = FirebaseDatabase.getInstance();

    mRef = database.getReference();
    FirebaseUser user = auth.getCurrentUser();
    userID = user.getUid();
btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String fname = etFname.getText().toString().trim();
            final String lname = etLname.getText().toString().trim();
            final String id_card = etIdCard.getText().toString().trim();
            final String email = etEmail.getText().toString().trim();
            final String mobile = etMobile.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            if (TextUtils.isEmpty(email)){
                Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(password)){
                Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(fname)){
                Toast.makeText(getApplicationContext(), "Enter FirstName!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (password.length() < 8) {
                Toast.makeText(getApplicationContext(), "Password too short, enter minimum 8 characters!", Toast.LENGTH_SHORT).show();
                return;
            }

            progressBar.setVisibility(View.VISIBLE);

            auth.createUserWithEmailAndPassword(email,password)
                    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {


                            if (!task.isSuccessful()) {

                                UserInfo userInfo = new UserInfo(fname,lname,id_card,mobile,email);
                                mRef.child("users").child(userID).setValue(userInfo);
                                etFname.setText("");
                                etLname.setText("");
                                etIdCard.setText("");
                                etMobile.setText("");
                                etEmail.setText("");
                                Toast.makeText(RegisterActivity.this, "Authentication failed." + task.getException(),
                                        Toast.LENGTH_SHORT).show();

                            } else {
                                Toast.makeText(RegisterActivity.this, "Register Successfully" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
                                progressBar.setVisibility(View.GONE);

                                finish();
                            }

                        }
                    });
        }
    });


}

And now get new error like this java.lang.NoSuchMethodError: No virtual method zzTt()Z in class Lcom/google/firebase/FirebaseApp; or its super classes (declaration of 'com.google.firebase.FirebaseApp' appears in /data/app/com.example.androiddev.army31-1/split_lib_dependencies_apk.apk:classes36.dex)

Now I can fix it because compile on gradle not same version

And got the same thing


回答1:


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




回答2:


Check this:

if(auth.getCurrentUser() != null){
    uid = auth.getCurrentUser().getUID();
}



回答3:


 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"
      }
    }
  }

}



来源:https://stackoverflow.com/questions/45448308/firebase-getuid-nullpointerexception-when-authen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!