Skip to content Skip to sidebar Skip to footer

Initializing Firebase Auth Object Error

I am getting an error whenever I try to run the app: Here is .java class: package com.safariagaming.flix; import android.app.ProgressDialog; import android.support.annotation.N

Solution 1:

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

This means your mAuth member variable is null. You probably never assigned a value to it. Assign it with mAuth = FirebaseAuth.getInstance() during onCreate() or at least some time before you call createUserWithEmailAndPassword on it.

Solution 2:

You should have defined the variables email and passwordoutside the method registerUser(). It seems that your Button is not correctly set as well.

package com.safariagaming.flix;

/**
 * Imports...
 */publicclassSignUpScreenextendsAppCompatActivityimplementsView.OnClickListener {

   privateStringemail=null;
   privateStringpassword=null;
   private FirebaseAuth.AuthStateListener mAuthListener;

   @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_sign_up_screen);

  /**
   * Inits and code...
   */// Signup button on listener
    buttonSignup.setOnClickListener(newView.OnClickListener() {
    @OverridepublicvoidonClick(View view) {
        email = editTextEmail.getText().toString().trim();
        password = editTextPassword.getText().toString().trim();

        // Verificationsif (TextUtils.isEmpty(email)) {
            Toast.makeText(LoginActivity.this, R.string.empty_email, Toast.LENGTH_SHORT).show();
            return;
        }

        if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            Toast.makeText(LoginActivity.this, R.string.wrong_email, Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(password)) {
            Toast.makeText(LoginActivity.this, R.string.empty_password, Toast.LENGTH_SHORT).show();
        }

        // Authenticating user
        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(LoginActivity.this, newOnCompleteListener<AuthResult>() {
                    @OverridepublicvoidonComplete(@NonNull Task<AuthResult> task) {


                        if (!task.isSuccessful()) {
                            Toast.makeText(LoginActivity.this, R.string.wrong_credentials, Toast.LENGTH_SHORT).show();
                        } else {
                            goDashboard();
                            finish();
                        }
                    }
                });
             }
         });
      }
   // onStart() state listener@OverridepublicvoidonStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }

    @OverridepublicvoidonStop() {
        super.onStop();
        if (mAuthListener != null) {
             mAuth.removeAuthStateListener(mAuthListener);
        }
    }
 }

Post a Comment for "Initializing Firebase Auth Object Error"