-2

i want to register a new user using firebaseAuth but i keep getting error

i have debugged using invalidate and restart and rebuilt the project, and even clean project yet it won't run successfully

RegisterActivity

public class RegisterActivity extends AppCompatActivity{

private EditText mNickname;
private EditText mEmail;
private EditText mPassword;
private TextView gotoLoginPage;
private Button mRegisterbutton;
private FirebaseAuth mAuth;
private ProgressDialog mRegProgress;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    mNickname = findViewById(R.id.registerNicknameId);
    mEmail = findViewById(R.id.registerEmailId);
    mPassword = findViewById(R.id.registerPasswordId);
    FirebaseApp.initializeApp(this);


    mRegisterbutton = findViewById(R.id.registerButton);
    gotoLoginPage = findViewById(R.id.gotoLoginPage);

   getSupportActionBar().setTitle("Create new account");
   getSupportActionBar().setDisplayHomeAsUpEnabled(true);

   mRegProgress = new ProgressDialog(this);

    mRegisterbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String nickname = mNickname.getText().toString();
            String email = mEmail.getText().toString();
            String password = mPassword.getText().toString();

            if (mNickname.equals("")|| mEmail.equals("") || mPassword.equals("")){
                Toast.makeText(getApplicationContext(), "Fill in the fields", Toast.LENGTH_LONG).show();
                mRegProgress.show();

                registerUser(email, password);
            }
            Intent toHomePage = new Intent(RegisterActivity.this, HomeActivity.class);
            startActivity(toHomePage);
            finish();
        }
    });

    gotoLoginPage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent tologinPage = new Intent(RegisterActivity.this, LoginActivity.class);
            startActivity(tologinPage);
        }
    });

}

private void registerUser(String email, String password) {
    mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()){
                Intent gotoHomePage = new Intent(RegisterActivity.this, HomeActivity.class);
                startActivity(gotoHomePage);
                finish();
            }
        }
    });
}

}

MainActivity

public class HomeActivity extends AppCompatActivity {

private FirebaseAuth mAuth;
private Toolbar  mToolbar;

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

    FirebaseApp.initializeApp(this);

    mToolbar =findViewById(R.id.toolbarId);
    getSupportActionBar().setTitle("PhorumChat");
}


private void sendToStart() {

    Intent goToWelcomePage = new Intent(HomeActivity.this, WelcomeActivity.class);
    startActivity(goToWelcomePage);
    finish();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.main_menu, menu);

 return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);

    if (item.getItemId() == R.id.main_logoutBtn){
        FirebaseAuth.getInstance().signOut();
        sendToStart();
    }

    return true;
}

@Override
public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();
    updateUI(currentUser);
}

private void updateUI(FirebaseUser currentUser) {
    if (currentUser ==null){
        Intent homepage= new Intent(HomeActivity.this, WelcomeActivity.class);
        startActivity(homepage);
        finish();
    }
}

}

This is the error i got when i run the program. Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object refererence

2kaybiel
  • 5
  • 3

1 Answers1

0

The error comes from:

public void onStart() {
    super.onStart();
    // Check if user is signed in (non-null) and update UI accordingly.
    FirebaseUser currentUser = mAuth.getCurrentUser();
    updateUI(currentUser);
}

Since you're not initializing mAuth anywhere, this is null.getCurrentUser(), which throws the error you get.

Either initialize mAuth in onCreate:

mAuth = FirebaseAuth.getInstance();

Or just replace mAuth with FirebaseAuth.getInstance() where you use it.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807