简体   繁体   中英

Kotlin App Crashing due to NULL POINTER EXCEPTION

I am trying to use Firebase's mobile authentication with my Kotlin app. My app keeps on crashing showing a NULL POINTER EXCEPTION, My best guess is that the 'code' variable remains null even though I assign it a value in fun onVerificationCompleted(credential: PhoneAuthCredential).

My Kotlin Code:

package com.example.myhospital

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import com.google.android.gms.tasks.Task
import com.google.firebase.FirebaseException
import com.google.firebase.FirebaseTooManyRequestsException
import com.google.firebase.auth.*
import kotlinx.android.synthetic.main.activity_otp_auth.*
import java.util.concurrent.TimeUnit

class otp_auth : AppCompatActivity() {

    lateinit var mauth:FirebaseAuth
    var storedVerificationId:String?=null
//    var resendToken:PhoneAuthProvider.ForceResendingToken?=null
    var code:String?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_otp_auth)

        mauth=FirebaseAuth.getInstance()

        bu_send_otp.setOnClickListener {
                view:View->verify()
        }


    }


    private fun verify(){
        val phnNo=enter_mobile.text.toString()

        PhoneAuthProvider.getInstance().verifyPhoneNumber(
            "+91"+phnNo, // Phone number to verify
            60, // Timeout duration
            TimeUnit.SECONDS, // Unit of timeout
            this, // Activity (for callback binding)
            mcallbacks) // OnVerificationStateChangedCallbacks
    }


    var mcallbacks = object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

        override fun onVerificationCompleted(credential: PhoneAuthCredential) {
            // This callback will be invoked in two situations:
            // 1 - Instant verification. In some cases the phone number can be instantly
            //     verified without needing to send or enter a verification code.
            // 2 - Auto-retrieval. On some devices Google Play services can automatically
            //     detect the incoming verification SMS and perform verification without
            //     user action.
            buVerify.setOnClickListener { view: View ->
                code = enter_OTP.text.toString()          //Assigned a value to code
            }
            signInWithPhoneAuthCredential(credential)
        }

        override fun onVerificationFailed(e: FirebaseException) {
            // This callback is invoked in an invalid request for verification is made,
            // for instance if the the phone number format is not valid.

            if (e is FirebaseAuthInvalidCredentialsException) {
                Toast.makeText(applicationContext,"Chal Be!!",Toast.LENGTH_SHORT).show()
            }
        }

        override fun onCodeSent(
            verificationId: String,
            token: PhoneAuthProvider.ForceResendingToken
        ) {
            // The SMS verification code has been sent to the provided phone number, we
            // now need to ask the user to enter the code and then construct a credential
            // by combining the code with a verification ID.
            super.onCodeSent(verificationId, token)

            // Save verification ID and resending token so we can use them later
            storedVerificationId = verificationId

        }
    }


    val credential = PhoneAuthProvider.getCredential(storedVerificationId!!, code!!)


    private fun signInWithPhoneAuthCredential(credential: PhoneAuthCredential) {
        mauth.signInWithCredential(credential).addOnCompleteListener{ task:Task<AuthResult> ->
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                val intent = Intent(this,book_appoint::class.java)
                startActivity(intent)

            } else {
                // Sign in failed, display a message and update the UI
                if (task.exception is FirebaseAuthInvalidCredentialsException) {
                    Toast.makeText(applicationContext,"WRONG!!",Toast.LENGTH_SHORT).show()
                }
            }
        }
    }


}

My logcat shows an error at :

val credential = PhoneAuthProvider.getCredential(storedVerificationId!!, code!!)

Caused by: kotlin.KotlinNullPointerException at com.example.myhospital.otp_auth.(otp_auth.kt:89)

My logcat:

2019-12-31 02:27:42.496 16222-16222/? E/mple.myhospita: Unknown bits set in runtime_flags: 0x8000
2019-12-31 02:27:45.804 16222-16222/com.example.myhospital E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myhospital, PID: 16222
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myhospital/com.example.myhospital.otp_auth}: kotlin.KotlinNullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        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)
     Caused by: kotlin.KotlinNullPointerException
        at com.example.myhospital.otp_auth.<init>(otp_auth.kt:89)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        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) 

My XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".otp_auth">

    <EditText
        android:id="@+id/enter_mobile"
        android:layout_width="359dp"
        android:layout_height="73dp"
        android:layout_marginTop="56dp"
        android:ems="10"
        android:hint="Enter Your Mobile No."
        android:inputType="phone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/bu_send_otp"
        android:layout_width="151dp"
        android:layout_height="56dp"
        android:layout_marginTop="28dp"
        android:text="Send OTP"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/enter_mobile" />

    <EditText
        android:id="@+id/enter_OTP"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter OTP"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/bu_send_otp"
        app:layout_constraintHorizontal_bias="0.235"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bu_send_otp"
        app:layout_constraintVertical_bias="0.246" />

    <Button
        android:id="@+id/buVerify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="68dp"
        android:layout_marginBottom="256dp"
        android:fontFamily="monospace"
        android:text="Verify"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.384"
        app:layout_constraintStart_toEndOf="@+id/enter_OTP" />

</androidx.constraintlayout.widget.ConstraintLayout>

We should know about the basic difference between "?" and "!!" operator. "?" will execute the program, even variable is null and "!!" produce a null pointer exception.

Here is one more operator ie Elvis operator that provides a meaning full value to a null value even if the variable is null.

So as per the requirement, You can try this code

var storedVerificationId:String?=null

val storeverificationIdEvenNull: String= if (storedVerificationId:String != null) storedVerificationId:String.length else "-1"

and change the code like

val credential = PhoneAuthProvider.getCredential(storedVerificationId, code)

now if the value is null then it will always assign "-1" as a String value in the storedVerificationId and you can perform the operation based on the ID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM