-1

I am using Firebase phone authentication and fetching the top with the help of a broadcast receiver to the app. Below I have shared the OTPAutoReader code and I am facing this issue -

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Class.isInterface()' on a null object reference
   at java.lang.Class.isAssignableFrom(Class.java:589)
   at android.os.Parcel.readParcelableCreatorInternal(Parcel.java:4882)
   at android.os.Parcel.readParcelableInternal(Parcel.java:4795)
   at android.os.Parcel.readValue(Parcel.java:4561)
   at android.os.Parcel.readValue(Parcel.java:4341)
   at android.os.Parcel.-$$Nest$mreadValue()
   at android.os.Parcel$LazyValue.apply(Parcel.java:4439)
   at android.os.Parcel$LazyValue.apply(Parcel.java:4398)
   at android.os.BaseBundle.getValueAt(BaseBundle.java:394)
   at android.os.BaseBundle.getValue(BaseBundle.java:374)
   at android.os.BaseBundle.getValue(BaseBundle.java:357)
   at android.os.BaseBundle.get(BaseBundle.java:696)
   at android.os.Bundle.getParcelable(Bundle.java:947)
   at com.adithya.memoneet.v1.presentation.auth.OTPAutoReader.onReceive(OTPAutoReader.kt:27)
   at android.app.ActivityThread.handleReceiver(ActivityThread.java:4544)
   at android.app.ActivityThread.-$$Nest$mhandleReceiver()
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2308)
   at android.os.Handler.dispatchMessage(Handler.java:106)
   at android.os.Looper.loopOnce(Looper.java:240)
   at android.os.Looper.loop(Looper.java:351)
   at android.app.ActivityThread.main(ActivityThread.java:8381)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)

Above issue is caused on this line -

 bundle.getParcelable(SmsRetriever.EXTRA_STATUS, Status::class.java)

of below code -

class OTPAutoReader : BroadcastReceiver() {

private var otpSmsListener: OTPSMSListener? = null

fun init(otpSmsListener: OTPSMSListener) {
    this.otpSmsListener = otpSmsListener
}

override fun onReceive(context: Context?, intent: Intent?) {
    if (SmsRetriever.SMS_RETRIEVED_ACTION == intent?.action) {
        val bundle: Bundle? = intent.extras
        if (bundle != null) {
            val status: Status? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                bundle.getParcelable(SmsRetriever.EXTRA_STATUS, Status::class.java)
            } else {
                bundle.getParcelable(SmsRetriever.EXTRA_STATUS)
            }
            if (status != null) {
                when (status.statusCode) {
                    CommonStatusCodes.SUCCESS -> {
                        val message: String? = bundle.getString(SmsRetriever.EXTRA_SMS_MESSAGE)
                        Log.i("TAG >> SUCCESS", message ?: "")
                        message?.let {
                            try {
                                val pattern = Pattern.compile("\\d{6}")
                                val matcher = pattern.matcher(message)
                                if (matcher.find()) {
                                    val otp = matcher.group(0)

                                    if (otp != null) {
                                        otpSmsListener?.onSuccess(otp)
                                    } else {
                                        otpSmsListener?.onFailure("OTP not receive")
                                    }
                                } else {
                                    otpSmsListener?.onFailure("OTP not receive")
                                }
                            } catch (e: Exception) {
                                e.printStackTrace()
                                otpSmsListener?.onFailure("Failed to retrieve SMS")
                            }
                        } ?: run {
                            otpSmsListener?.onFailure("Failed to retrieve SMS")
                        }
                    }

                    CommonStatusCodes.TIMEOUT -> {
                        Log.i("TAG >> TIMEOUT", "timeout")
                        otpSmsListener?.onFailure("timeout")
                    }
                }
            }
        }
    }
}

interface OTPSMSListener {
    fun onSuccess(otp: String)
    fun onFailure(msg: String)
}
}

Note - This issue is only faced on android 13.

Priyanshu
  • 101
  • 7

1 Answers1

0

This appears similar to this question, where the solution was to use BundleCompat.getParcelable(Bundle, String, Class) from androidx.core:core-ktx:1.10.0 instead of getParcelable(String, Class).

hemisphire
  • 1,205
  • 9
  • 19