1
votes

Here's the code -

    public class login extends AppCompatActivity implements View.OnClickListener {

    private Button signIn;
    private ProgressDialog progressDialog;
    private EditText emailID;
    private EditText password1;
    private TextView emailLabel;
    private TextView passwordLabel;

    private FirebaseAuth firebaseAuth;

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

        firebaseAuth = FirebaseAuth.getInstance();

        progressDialog = new ProgressDialog(this);

        emailID = (EditText)findViewById(R.id.txtFieldEmailID);
        password1 = (EditText) findViewById(R.id.txtFieldPassword);
        emailLabel = (TextView) findViewById(R.id.labelEmail);
        passwordLabel = (TextView) findViewById(R.id.labelPassword);

        signIn.setOnClickListener(this);
    }

    private void loginUser(){
        String email = emailID.getText().toString().trim();
        String password = password1.getText().toString().trim();

        if(TextUtils.isEmpty(email)){
            //email field is empty
            Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
            return;
        }

        if(TextUtils.isEmpty(password)){
            //password is empty
            Toast.makeText(this,"Please enter your password",Toast.LENGTH_LONG).show();
            return;
        }

        //if validations are ok
        //show a progressbar
        firebaseAuth.signInWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    public static final String TAG ="";

                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            // Sign in success, update UI with the signed-in user's information
                            Log.d(TAG, "signInWithEmail:success");
                            FirebaseUser user = firebaseAuth.getCurrentUser();
                            updateUI(user);
                        } else {
                            // If sign in fails, display a message to the user.
                            Log.w(TAG, "signInWithEmail:failure", task.getException());
                            Toast.makeText(login.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                            updateUI(null);
                        }

                        // ...
                    }

                    private void updateUI(FirebaseUser user) {
                        progressDialog.setMessage("User has signed in");
                        progressDialog.show();

                    }
                });




    }

    @Override
    public void onClick(View view) {
        if(view == signIn){
            loginUser();
        }
    }  
  }

Error that I am getting on starting the application - 01-13 17:07:13.543 5027-5058/com.example.kinnari.trial2 D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=2716, firebase_screen_class(_sc)=MainActivity, firebase_screen_id(_si)=-7572526684473257642}] 01-13 17:07:13.734 5027-5027/com.example.kinnari.trial2 D/AndroidRuntime: Shutting down VM 01-13 17:07:13.737 5027-5027/com.example.kinnari.trial2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.kinnari.trial2, PID: 5027 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kinnari.trial2/com.example.kinnari.trial2.login}: 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:2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 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.kinnari.trial2.login.onCreate(login.java:46) at android.app.Activity.performCreate(Activity.java:6999) at android.app.Activity.performCreate(Activity.java:6990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6494)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)  01-13 17:07:13.851 5027-5047/com.example.kinnari.trial2 I/zygote: Background concurrent copying GC freed 6839(912KB) AllocSpace objects, 3(124KB) LOS objects, 50% free, 1707KB/3MB, paused 6.740ms total 111.655ms 01-13 17:24:04.987 5027-5054/com.example.kinnari.trial2 W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.

2

2 Answers

0
votes

This has nothing to do with Firebase Authentication. You just haven't assigned a value to signIn, and when you call setOnClickListener() on it, it's throwing a NullPointerException. Give it a non-null value before calling methods on it.

0
votes

You forgot to initialize the signIn button before the line:

signIn.setOnClickListener(this);

Because of this, the button always has a null value and that's what is causing your app to crash.

Adding signIn = findViewById(R.id.yourButtonID); before the line above will fix your problem. I don't think there's a problem with the rest of your code.

I hope this helps.. Merry coding!