• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps App crashes

LogCat:

Code:
07-12 17:10:21.749 30033-30033/com.example.jopayne1968.firebasetest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                      Process: com.example.jopayne1968.firebasetest, PID: 30033
                                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jopayne1968.firebasetest/com.example.jopayne1968.firebasetest.LogIn}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3256)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
                                                                                          at android.app.ActivityThread.access$1100(ActivityThread.java:223)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                          at android.os.Looper.loop(Looper.java:158)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:7231)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference
                                                                                          at com.example.jopayne1968.firebasetest.LogIn.onCreate(LogIn.java:31)
                                                                                          at android.app.Activity.performCreate(Activity.java:6877)
                                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3209)
                                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3352)
                                                                                          at android.app.ActivityThread.access$1100(ActivityThread.java:223)
                                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                          at android.os.Looper.loop(Looper.java:158)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:7231)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)

Code:

Code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button buttonRegister;
    private EditText editTextEmail;
    private EditText editTextPassword;
    private Button SignUp;
    private ProgressDialog progressDialog;
    private FirebaseAuth firebaseAuth;

    [USER=1021285]@override[/USER]
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firebaseAuth = FirebaseAuth.getInstance();
        progressDialog = new ProgressDialog(this);


        editTextEmail = (EditText) findViewById(R.id.EditTextEmail);
        editTextPassword = (EditText) findViewById(R.id.EditTextPassword);

        buttonRegister = (Button) findViewById(R.id.ButtonRegisterUser);
        buttonRegister.setOnClickListener(this);
        SignUp = (Button) findViewById(R.id.TextViewSignIn);
        SignUp.setOnClickListener(this);

        SignUp.setOnClickListener(new View.OnClickListener() {

            [USER=1021285]@override[/USER]
            public void onClick(View v) {
                startActivity(new Intent(getApplicationContext(), LogIn.class));
            }

        });
    }


    private void registerUser() {
        String Email = editTextEmail.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();

        if (TextUtils.isEmpty(Email)) {
            Toast.makeText(this, "Please enter an emai", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "Please enter an password", Toast.LENGTH_SHORT).show();
            return;
        }

        progressDialog.setMessage("Registering User ...");
        progressDialog.show();
        firebaseAuth.createUserWithEmailAndPassword(Email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    [USER=1021285]@override[/USER]
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            finish();
                            if (firebaseAuth.getCurrentUser() != null) {
                                Toast.makeText(MainActivity.this, "Registered", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
                            }
                        } else {
                            Toast.makeText(MainActivity.this, "Failed to register", Toast.LENGTH_SHORT).show();
                            progressDialog.hide();
                        }
                    }
                });
    }

    [USER=1021285]@override[/USER]
    public void onClick(View view) {

        if (view == buttonRegister) {
            registerUser();
        }

        if (view == SignUp) {

            startActivity(new Intent(this, LogIn.class));
        }
    }
}
 
Upvote 0
The error relates to class LogIn, which you have not shown.
From the stack trace:

Code:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference
at com.example.jopayne1968.firebasetest.LogIn.onCreate(LogIn.java:31)
 
  • Like
Reactions: scary alien
Upvote 0
Code:
public class LogIn extends AppCompatActivity implements View.OnClickListener  {
    private Button ButtonSignUp;
    private EditText editTextEmail;
    private EditText editTextPassword;
    private TextView TextViewSignUp2;
    private ProgressDialog progressDialog;
    private FirebaseAuth firebaseAuth;
    [USER=1021285]@override[/USER]
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);
if (firebaseAuth.getCurrentUser()!=null){
    //profile activity
    startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}
        ButtonSignUp = (Button)findViewById(R.id.ButtonRegisterUser);
        editTextEmail = (EditText)findViewById(R.id.EditTextEmail);
        editTextPassword = (EditText) findViewById(R.id.EditTextPassword);

        firebaseAuth  = FirebaseAuth.getInstance();
        progressDialog = new ProgressDialog(this);

        ButtonSignUp.setOnClickListener(this);
        TextViewSignUp2.setOnClickListener(this);

    }
    private void userLogIn() {
        String Email = editTextEmail.getText().toString().trim();
        String password = editTextPassword.getText().toString().trim();

        if (TextUtils.isEmpty(Email)) {
            Toast.makeText(this, "Please enter an emai", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "Please enter an password", Toast.LENGTH_SHORT).show();
            return;
        }
        progressDialog.setMessage("Logging In");
        progressDialog.show();

        firebaseAuth.signInWithEmailAndPassword(Email , password)
        .addOnCompleteListener(this , new OnCompleteListener<AuthResult>() {
            [USER=1021285]@override[/USER]
            public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
    finish();
    startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}
            }
        });
    }
    [USER=1021285]@override[/USER]
    public void onClick(View v) {
        if (v ==ButtonSignUp) {
            userLogIn();
            progressDialog.hide();
        }
        if (v ==TextViewSignUp2){
            finish();
            progressDialog.hide();
            startActivity(new Intent(this , MainActivity.class));
        }
    }
}
 
Upvote 0
Code:
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);
if (firebaseAuth.getCurrentUser()!=null){      <--------------------

At this point, variable firebaseAuth is null. Trying to invoke a method on a null object generates a NullPointerException

You should probably move this line

Code:
firebaseAuth  = FirebaseAuth.getInstance();

So it gets executed before the 'if' statement in the above code.
 
  • Like
Reactions: scary alien
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones