Jagmaster
Jagmaster

Reputation: 107

Send password reset email method crashes the app

I am trying to make a forget password activity where there is an EditText field for user to input their e-mail and click a Button and the sendPasswordResetEmail() method will be invoked... So the code that I have is this

mAuth = FirebaseAuth.getInstance();
resetPasswordBtn = (Button)findViewById(R.id.resetPasswordButton);
resetPasswordFld = (EditText)findViewById(R.id.resetPasswordField);

final String email = resetPasswordFld.getText().toString().trim();

resetPasswordBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        mAuth.sendPasswordResetEmail(email)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            Toast.makeText(ResetPassword.this, "e-mail buat reset password sudah dikirimkan",Toast.LENGTH_LONG).show();
                            Intent loginIntent = new Intent (ResetPassword.this, LoginActivity.class);
                            startActivity(loginIntent);
                        }
                    }
                });
    }
});

When I tried it, the app crashes, and I got this error log

03-23 16:28:59.829 6990-6990E/UncaughtException: java.lang.IllegalArgumentException: Given String is empty or null
    at com.google.android.gms.common.internal.zzac.zzdv(Unknown Source)
    at com.google.firebase.auth.FirebaseAuth.sendPasswordResetEmail(Unknown Source)
    at com.ResetPassword$1.onClick(ResetPassword.java:37)
    at android.view.View.performClick(View.java:6207)
    at android.widget.TextView.performClick(TextView.java:11094)
    at android.view.View$PerformClick.run(View.java:23639)
    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:6688)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
03-23 16:28:59.833 6990-7024 D/FA: Logging event (FE): _ae, Bundle[{_o=crash, _sc=ResetPassword, _si=-8440426569227389093, timestamp=1490261339829, fatal=1}]
03-23 16:29:00.128 6990-6990 E/AndroidRuntime: FATAL EXCEPTION: main
 Process: , PID: 6990
 java.lang.IllegalArgumentException: Given String is empty or null
 at com.google.android.gms.common.internal.zzac.zzdv(Unknown Source)
 at com.google.firebase.auth.FirebaseAuth.sendPasswordResetEmail(Unknown Source)
 at.ResetPassword$1.onClick(ResetPassword.java:37)
 at android.view.View.performClick(View.java:6207)
 at android.widget.TextView.performClick(TextView.java:11094)
 at android.view.View$PerformClick.run(View.java:23639)
 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:6688)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

I am actually convinced that I am doing this wrong as mAuth will definitely returns null as no user is logged in right? I am having difficulties finding good documentation about this and the documentation in the firebase documentations does not really help.

There must be a way to reference a database to check if the email address inputted really exists... But my rules prevent users that aren't logged in to access them... So I am just really confused on how to get started with this...

Upvotes: 1

Views: 2855

Answers (1)

koceeng
koceeng

Reputation: 2165

I think the error comes from this line:

mAuth.sendPasswordResetEmail(email)

And because you mark email as final, it doesn't get updated when onClick is happening. And email value is always empty because at first the resetPasswordFld is indeed empty. So you should do it this way:

// I believe resetPasswordFld is accessible from inside OnClickListener, so

resetPasswordBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        // place this code here, rather than make it final and place it outside
        String email = resetPasswordFld.getText().toString().trim();

        mAuth.sendPasswordResetEmail(email) ...
    }
}

Upvotes: 3

Related Questions