0

Crash:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.online_exam.quizapp, PID: 15277
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.online_exam.quizapp/com.example.online_exam.quizapp.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2740)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2801)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1540)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:163)
        at android.app.ActivityThread.main(ActivityThread.java:6358)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:799)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.online_exam.quizapp.LoginActivity.onCreate(LoginActivity.java:53)
        at android.app.Activity.performCreate(Activity.java:6840)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2801) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1540) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:163) 
        at android.app.ActivityThread.main(ActivityThread.java:6358) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:799) 
I/Process: Sending signal. PID: 15277 SIG: 9 
Process 15277 terminated.

enter image description here

This is my Login Activity. In my app it starts up to login activity and after I login the app crashes suddenly.

EditText emailId, password;
Button btnSignIn;
TextView tvSignUp;
FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;

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

    mFirebaseAuth = FirebaseAuth.getInstance();
    emailId = (EditText) findViewById(R.id.editText);
    password = (EditText) findViewById(R.id.editText2);
    btnSignIn = (Button) findViewById(R.id.button2);
    tvSignUp = (TextView) findViewById(R.id.textView);

    mAuthStateListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser mFirebaseUser = mFirebaseAuth.getCurrentUser();
            if (mFirebaseUser != null) {
                Toast.makeText(LoginActivity.this, "You Are Logged In", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(i);
            } else {
                Toast.makeText(LoginActivity.this, "Please Login", Toast.LENGTH_SHORT).show();
            }

        }
    };

    btnSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email = emailId.getText().toString();
            String pwd = password.getText().toString();
            if (email.isEmpty()) {
                emailId.setError("Please enter email");
                emailId.requestFocus();
            } else if (pwd.isEmpty()) {
                password.setError("please enter your password");
                password.requestFocus();
            } else if (email.isEmpty() && pwd.isEmpty()) {
                Toast.makeText(LoginActivity.this, "Fields are Empty", Toast.LENGTH_SHORT).show();
            } else if (!(email.isEmpty() && pwd.isEmpty())) {
                mFirebaseAuth.signInWithEmailAndPassword(email, pwd).addOnCompleteListener(LoginActivity.this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (!task.isSuccessful()) {
                            Toast.makeText(LoginActivity.this, "Login error", Toast.LENGTH_SHORT).show();
                        } else {
                            Intent intToMain = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intToMain);
                        }
                    }
                });
            } else {
                Toast.makeText(LoginActivity.this, "Error Occured!", Toast.LENGTH_SHORT).show();
            }
        }
    });

    tvSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intSignup = new Intent(LoginActivity.this, SignupActivity.class);
            startActivity(intSignup);
        }
    });
}

@Override
protected void onStart() {
    super.onStart();
    mFirebaseAuth.addAuthStateListener(mAuthStateListener);
}

enter image description here

This is my activity of login. I don't know what the error is coming from.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • This looks like your view variable is not correctly inited before call setOnClickListener. – Tshunglee Oct 17 '19 at 03:52
  • 2
    No one will be able to help completely if you don't post the code causing this crash, but from the looks of it, you tried to set an on-click listener on a null object, causing a null pointer exception. – Luke Redmore Oct 17 '19 at 03:53
  • 1
    Check whether your view is properly initialised on which you have invoked onClickListener, and please edit your question to share your code of Login – Molly Oct 17 '19 at 03:57
  • Post your Activity code here or at least onCreate code here. – Arun Shankar Oct 17 '19 at 04:00
  • 2
    Possible duplicate of [Null pointer Exception - findViewById()](https://stackoverflow.com/questions/19078461/null-pointer-exception-findviewbyid) – Vivek Mishra Oct 17 '19 at 04:01
  • Without knowing your code, it is not possible to help you to fix error. – DHAVAL A. Oct 17 '19 at 04:05
  • sorry guys...i have added my login activity details now.. – Vaibhav Tode Oct 17 '19 at 04:57

4 Answers4

0

You didn't initialize btnSignIn

//initialize before setOnClickListener
btnSignIn = findViewById(R.id.yourbuttonid)  //yourbuttonid = id of button in xml
Ranjeet Chouhan
  • 686
  • 5
  • 13
0

Check the layout.xml file button ids that exist or not

Your App is crashing because your clicked button is null, needs to initialize before use it.

*check id=button2 is exist in your xml file *
btnSignIn = (Button) findViewById(R.id.button2);

*check id=textView is exist in your xml file *
tvSignUp = (TextView) findViewById(R.id.textView);
Jane Alam
  • 360
  • 2
  • 7
0

Check your Button findviewbyid it seems wrong in your code

You can try this

 btnSignIn = (Button) findViewById(R.id.button2);

or


publick yourclass implements View.OnclickListner{

 btnSignIn = (Button) findViewById(R.id.button2);
btnSignIn.setOnclickListner(this);


}

 @Override
        public void onClick(View v) {

// implement your code logic here

           }

SAVALIYA REENA
  • 218
  • 2
  • 15
Praful Korat
  • 374
  • 4
  • 22
0

Initialize your button before assigning onClick event

//in activity
 yourButton= (Button) findViewById(R.id.btnId);

//In fragment
 yourButton= (Button) view.findViewById(R.id.btnId);
Venkatesh
  • 190
  • 4
  • 9