Issue
This worked perfectly fine from the time I created the project. But suddenly is starts crashing. The sign up activity works fine. But this sign in activity is crashing.
The error shown by the debugger.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.durden, PID: 7921
java.lang.NullPointerException
at com.example.durden.activity.SignInActivity.onCreate$lambda-2(SignInActivity.kt:49)
at com.example.durden.activity.SignInActivity.$r8$lambda$ZBrxDCm4vr69l-PuE690AA_KCF8(Unknown Source:0)
at com.example.durden.activity.SignInActivity$$ExternalSyntheticLambda1.onClick(Unknown Source:4)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
The signin Activity
class SignInActivity : AppCompatActivity() {
// getting the references from firebase
private var auth: FirebaseAuth? = null
private var firebaseUser: FirebaseUser? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// gets the view binding of sign_in_activity
val binding = ActivitySignInBinding.inflate(layoutInflater)
setContentView(binding.root)
// ends this activity and goes to sign up page if needed
binding.gotoSignUp.setOnClickListener {
val intent = Intent(
this@SignInActivity,
SignUpActivity::class.java
)
startActivity(intent)
finish()
}
binding.btnSignIn.setOnClickListener {
// storing the entered email and password
val email = binding.SImail.text.toString()
val password = binding.SIpass.text.toString()
// checks if the values are entered
if (TextUtils.isEmpty(email) && TextUtils.isEmpty(password)) {
Toast.makeText(applicationContext,"email and password are required", Toast.LENGTH_SHORT).show()
}
// signs in the user using firebase data
else {
auth!!.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this) {
if (it.isSuccessful) {
binding.SImail.setText("")
binding.SIpass.setText("")
// after sign in takes to user page
val intent = Intent(
this@SignInActivity,
UserActivity::class.java
)
startActivity(intent)
finish()
}
else {
Toast.makeText( applicationContext, "Invalid password or email", Toast.LENGTH_SHORT).show()
}
}
}
}
}
}
Firebase Authentication causing Android fatal exception
This sounds similar to my problem but still, I couldn't figure why it happens so.
Solution
You never initialize auth
, which means that by the time you execute auth!!.signInWithEmailAndPassword(email, password)
you get an exception.
Initialize auth
like shown in step 2 here in your onCreate
to prevent the error.
// Initialize Firebase Auth
auth = Firebase.auth
NullPointerException
s are very common, so I highly recommend learning how to troubleshoot these yourself. For that, check out:
- What is the Kotlin double-bang (!!) operator?
- Unfortunately MyApp has stopped. How can I solve this?
- What is a NullPointerException, and how do I fix it?
Answered By - Frank van Puffelen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.