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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Регистрация");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        username = findViewById(R.id.username);
        email = findViewById(R.id.email);
        password = findViewById(R.id.password);
        btn_register = findViewById(R.id.btn_register);

        auth = FirebaseAuth.getInstance();

        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            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>() {
                    @Override
                    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>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                    if(task.isSuccessful()){

                                        Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                        finish();
                                    }
                                }
                            });
                        }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.",
                            Toast.LENGTH_SHORT).show();

Leave a Reply

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