Validate username and email crashed and cannot insert to firebase database

The apps cannot insert any data to the database while this line of code makes error to the apps.

ref.child(uid).orderByChild("username").equalTo(validateName).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if(dataSnapshot.exists()){
                    Toast.makeText(Register.this, "Username Taken. Please try another one", LENGTH_SHORT).show();
                    return;
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

Besides, the uid is the code gather the email from the email authentication part.

private FirebaseAuth auth = FirebaseAuth.getInstance();
private FirebaseUser ur = auth.getCurrentUser();
db = FirebaseDatabase.getInstance();
    ref = db.getReference();
    if (ur != null){
        uid = ur.getEmail();
    }

The error from logcat

FATAL EXCEPTION: main
                                                                   Process: com.example.rex.ota30, PID: 30205
                                                                   com.google.firebase.database.DatabaseException: Invalid Firebase Database path: [email protected]. Firebase Database paths must not contain '.', '#', '$', '[', or ']'
                                                                       at com.google.android.gms.internal.zzbqh.zzjm(Unknown Source)
                                                                       at com.google.android.gms.internal.zzbqh.zzjn(Unknown Source)
                                                                       at com.google.firebase.database.DatabaseReference.child(Unknown Source)
                                                                       at com.example.rex.ota30.Register$4.onComplete(Register.java:203)
                                                                       at com.google.android.gms.tasks.zzc$1.run(Unknown Source)
                                                                       at android.os.Handler.handleCallback(Handler.java:751)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Solution 1:

As your error said, Firebase Database paths must not contain '.', '#', '$', '[', or ']'. This means that Firebase does not allow you use in the key symbols those symbols. Because of that, you need to econde the email address like this:

[email protected] -> name@email,com

To achieve this, i recomand you using the following methods:

static String encodeUserEmail(String userEmail) {
    return userEmail.replace(".", ",");
}

static String decodeUserEmail(String userEmail) {
    return userEmail.replace(",", ".");
}