android – Firebase doesn't register new users-ThrowExceptions

Exception or error:

I have a problem, I can’t register in my chat app. It shows me that “You can’t register”. I don’t know where is the problem. I set read and write to true in my database but it still not working. I use the last versions of Firebase. I haven’t problems with the internet and connected firebase to my project successfully.

This is my register activity

    MaterialEditText username,  email, password;

    Button btn_register;

    FirebaseAuth auth;
    DatabaseReference reference;

    protected void onCreate(Bundle savedInstanceState) {

        Toolbar toolbar = findViewById(;

        username = findViewById(;
        email = findViewById(;
        password = findViewById(;
        btn_register = findViewById(;

        auth = FirebaseAuth.getInstance();

        btn_register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String txt_username = username.getText().toString();
                String txt_email = email.getText().toString();
                String txt_password = password.getText().toString();

                if (TextUtils.isEmpty(txt_username)  || TextUtils.isEmpty(txt_email)  ||  TextUtils.isEmpty(txt_password)) {

                    Toast.makeText(RegisterActivity.this, "Write more", Toast.LENGTH_SHORT).show();
                } else if (txt_password.length() < 6 ){
                    Toast.makeText(RegisterActivity.this, "Password too short ", Toast.LENGTH_SHORT).show();
                } else {
                    register(txt_username, txt_email, txt_password);

    private void register(final String username, String email, String password){
        auth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()){
                            FirebaseUser firebaseUser= auth.getCurrentUser();
                            assert firebaseUser !=null;
                            String userid = firebaseUser.getUid();

                            reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);

                            HashMap<String, String> HashMap = new HashMap<>();
                            HashMap.put("id", userid);
                            HashMap.put("username", username);
                            HashMap.put("imageURL", "default");

                            reference.setValue(HashMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                                public void onComplete(@NonNull Task<Void> task) {

                                        Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        }else {
                            Toast.makeText(RegisterActivity.this, "You cant register", Toast.LENGTH_SHORT).show();

How to solve:

When initializing your FirebaseDatabase reference, the item node with the provided user id does not exist yet in your database thus the reference is null

String userid = firebaseUser.getUid();

reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);

Instead, initialize the reference to the Users node

String userid = firebaseUser.getUid();

reference = FirebaseDatabase.getInstance().getReference("Users");

Then when saving the user’s data you can save to their userId:

reference.child(userid).setValue(HashMap).addOnCompleteListener(new OnCompleteListener<Void>() {


in manifest file allow internet permission. if you continue this err create failure listener and print failure code

 Log.w(TAG, "createUserWithEmail:failure", task.getException());
                    Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",

Leave a Reply

Your email address will not be published. Required fields are marked *