0

I'm following this tutorial to explore Firebase and use email authentication using three java classes

  1. MainActivity
  2. RegisterActivity
  3. ProfileActivity

and three layout views.

  1. activity_main.xml
  2. activity_register.xml
  3. profilepage.xml

[Youtube tutorial]1 I'm getting

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.firebaselearning/com.example.firebaselearning.RegisterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

full error

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.firebaselearning, PID: 8008
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.firebaselearning/com.example.firebaselearning.RegisterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.example.firebaselearning.RegisterActivity.onCreate(RegisterActivity.java:60)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

following are the codes (excluding gradle because i don't think it's where error originates from)

MainActivity.java

public class MainActivity extends AppCompatActivity {

//views
Button mRegisterBtn, mLogInBtn;

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

    //initialise view
    mRegisterBtn = findViewById(R.id.registerbutton);
    mLogInBtn = findViewById(R.id.login);

    //handle register button click
    mRegisterBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //start register activity
            startActivity(new Intent(MainActivity.this, RegisterActivity.class));
        }
    });
  }
}

RegisterActivity.java

public class RegisterActivity extends AppCompatActivity {

//views
EditText mEmailET, mPassword;
Button mRegistrationBTN;

//progressbar to display progress which registering
ProgressDialog progressDialog;
//declare instance of fireAuth
private FirebaseAuth mAuth;

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

    //actionbar and its title
    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle("Create Account");

    //enable back button
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowHomeEnabled(true);

    //init
    mEmailET = findViewById(R.id.editTextTextEmailAddress);
    mPassword = findViewById(R.id.editTextTextPassword);
    mRegistrationBTN = findViewById(R.id.registerbutton);

    //initialising firebaseAuth instance
    mAuth = FirebaseAuth.getInstance();

    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Registering user ...");


    //handle register btn click
    mRegistrationBTN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //input email, password
            String email = mEmailET.getText().toString().trim();
            String password = mPassword.getText().toString().trim();
            //validate
            if(Patterns.EMAIL_ADDRESS.matcher(email).matches()){
                //show error and focus to edittext
                mEmailET.setError("Invalid Email");
                mEmailET.setFocusable(true);
            }else if (password.length()<6){
                //set error and focus to password
                mPassword.setError("Password length must be at least 6 characters");
                mPassword.setFocusable(true);
            }else{
                //register the user
                registerUser(email,password);
            }

        }
    });

}

private void registerUser(String email, String password){
    //email and password is valid ,shows progress dialog and start registering user
    progressDialog.show();

    mAuth.createUserWithEmailAndPassword(email,password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, dismiss dialog and start register activity
                        progressDialog.dismiss();
                        FirebaseUser user = mAuth.getCurrentUser();
                        assert user != null;
                        Toast.makeText(RegisterActivity.this,"Registered..."+user.getEmail(), Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(RegisterActivity.this, ProfileActivity.class));
                        finish();
                    } else {
                        // If sign in fails, display a message to the user.
                        progressDialog.dismiss();
                        Toast.makeText(RegisterActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
                    }
                }
            }) .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            //error, dismiss progrss dialog and get and show error message
            progressDialog.dismiss();
            Toast.makeText(RegisterActivity.this,""+e.getMessage(),Toast.LENGTH_SHORT).show();
        }
    });
}
@Override
public boolean onSupportNavigateUp() {
    onBackPressed(); //go previous activity on clicking back
    return super.onSupportNavigateUp();
  }
}

I've reverified that names of my xml files are same .

KAST
  • 3
  • 3
  • at which line it error ? – Ticherhaz FreePalestine Apr 25 '21 at 21:31
  • I've resolved the error , i accidently used "@+id/registerbutton" twice while layout constraints.. – KAST Apr 25 '21 at 22:53
  • also , due to referencing wrong button from wrong java class .the error arose because of referencing register button in activity_main.xml from RegisterActivity.java which has layout view of activity_register.xml with another button named register – KAST Apr 25 '21 at 22:56

1 Answers1

0

Error arose due to referencing register button from activity_main.xml layout which is view of MainActivity , from RegisterActivity which has another register button in it's view of activity_register.xml

sorry to waste your time . the register button from activity_main thus wasn't initialised in the Register.java

KAST
  • 3
  • 3