Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase .getUID NullPointerException when authen

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

enter image description here

like image 680
C.Junior Avatar asked Aug 01 '17 21:08

C.Junior


2 Answers

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

like image 53
Jawad Hassan Soomro Avatar answered Oct 22 '22 15:10

Jawad Hassan Soomro


Check this:

if(auth.getCurrentUser() != null){
    uid = auth.getCurrentUser().getUID();
}
like image 2
Juanma Perez Avatar answered Oct 22 '22 14:10

Juanma Perez