Error trying to execute firebase forgot-password

0

I'm doing the firebase's forgot password screen and it's giving an error when I put the .sendPasswordResetEmail.

This is the version of my firebase:

implementation 'com.google.firebase:firebase-core:11.8.0'
implementation 'com.google.firebase:firebase-database:11.8.0'
implementation 'com.google.firebase:firebase-storage:11.8.0'
implementation 'com.google.firebase:firebase-auth:11.8.0'
implementation 'com.firebaseui:firebase-ui-storage:0.6.0'

This is the error when I click the button:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task com.google.firebase.auth.FirebaseAuth.sendPasswordResetEmail(java.lang.String)' on a null object reference

This is my forgot password activity:

    public class EsqueceuSenhaActivity extends AppCompatActivity {

    private EditText editEmailEsqueceu;
    private Button botaoEnviarEsqueceu;
    private FirebaseAuth auth;

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

        editEmailEsqueceu = (EditText) findViewById(R.id.edit_esqueceu_email);
        botaoEnviarEsqueceu = (Button) findViewById(R.id.bt_esqueceu_enviar);

        botaoEnviarEsqueceu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editEmailEsqueceu.getText().toString() == null){

                    Toast.makeText(EsqueceuSenhaActivity.this, "Por favor digite seu email no campos!", Toast.LENGTH_SHORT).show();

                }else{

                    String email = editEmailEsqueceu.getText().toString();

                    auth.sendPasswordResetEmail(email).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {

                            if (task.isSuccessful()){

                                Toast.makeText(EsqueceuSenhaActivity.this, "Recuperação de senha inicada. Email enviado!", Toast.LENGTH_SHORT).show();

                            }else{

                                Toast.makeText(EsqueceuSenhaActivity.this, "Falha ao enviar email, tente novamente!", Toast.LENGTH_SHORT).show();

                            }

                        }
                    });

                }
            }
        });
    }
}

xml:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_degrade"
    tools:context=".activity.EsqueceuSenhaActivity">

    <ImageView
        android:id="@+id/imageViewEsqueceu"
        android:layout_width="256dp"
        android:layout_height="63dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="128dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/edit_esqueceu_email"
        app:layout_constraintEnd_toEndOf="@+id/edit_esqueceu_email"
        app:layout_constraintStart_toStartOf="@+id/edit_esqueceu_email"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.527"
        app:srcCompat="@drawable/logo" />

    <EditText
        android:id="@+id/edit_esqueceu_email"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="104dp"
        android:ems="10"
        android:hint="Digite seu e-mail"
        android:inputType="textEmailAddress"
        android:textColor="@color/textColorSecondy"
        app:layout_constraintBottom_toTopOf="@+id/bt_esqueceu_enviar"
        app:layout_constraintEnd_toEndOf="@+id/bt_esqueceu_enviar"
        app:layout_constraintStart_toStartOf="@+id/bt_esqueceu_enviar" />

    <Button
        android:id="@+id/bt_esqueceu_enviar"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="152dp"
        android:background="@drawable/backgroud_botao_login"
        android:text="@string/botao_logar"
        android:textColor="@color/textColorButton"
        android:theme="@style/botaoPadrao"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>
    
asked by anonymous 28.11.2018 / 19:36

1 answer

2

Luis,

Apparently you are sending an object with null reference.

You did not instantiate your "auth" variable.

Just instantiate it this way in your onCreate() :

auth = FirebaseAuth.getInstance();
    
28.11.2018 / 20:03