Error to register and login

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference

public class RegisterActivity extends AppCompatActivity {
    private EditText email,name,password;
    DatabaseReference databaseReference;
    FirebaseAuth mAuth;
    private DatabaseReference userIdRef;
    ProgressDialog registerDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mAuth=FirebaseAuth.getInstance();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        email=(EditText)findViewById(R.id.email);
        name=(EditText)findViewById(R.id.name);
        password=(EditText)findViewById(R.id.password);
        databaseReference= FirebaseDatabase.getInstance().getReference().child("chat_users");
      //  mAuth=FirebaseAuth.getInstance();
        registerDialog=new ProgressDialog(this);
        registerDialog.setMessage("Registering..");
    }
    public void submit(View view) {
        registerDialog.show();
        mAuth.createUserWithEmailAndPassword(email.getText().toString(),password.getText().toString()).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isComplete())
                {
                    registerDialog.dismiss();
                    Toast.makeText(getApplicationContext(),"Registered successfully",Toast.LENGTH_SHORT).show();
                    userIdRef=databaseReference.child(mAuth.getCurrentUser().getUid().toString());
                    userIdRef.child("name").setValue(name.getText().toString());
                    finish();
                }
                registerDialog.dismiss();
            }
        });
    }
}

1 answer

  • answered 2017-08-12 09:47 Wilik

    There's probably an error in the registration process, some examples are because of the password's length is not long enough and the Password authentication is not enabled yet in the Firebase Console.

    You can catch the error by modifying the onComplete method implementation to

    mAuth.createUserWithEmailAndPassword(email.getText().toString(),password.getText().toString()).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                // Register success
                Toast.makeText(getApplicationContext(), "Registered successfully", Toast.LENGTH_SHORT).show();
    
                userIdRef = databaseReference.child(mAuth.getCurrentUser().getUid());
                userIdRef.child("name").setValue(name.getText().toString());
                finish();
            } else {
                // If register fails
                Toast.makeText(getApplicationContext(), "Registration Error: " + task.getException(),Toast.LENGTH_SHORT).show();
            }
            registerDialog.dismiss();
        }
    });
    

    Hope this helps :)