Sahar
Sahar

Reputation: 311

TelephonyManager Crashs on Android 10

I am getting the below stack trace in my Crashlytics for wide range of android 10 devices. Fatal Exception: java.lang.NullPointerException Attempt to invoke virtual method 'java.lang.Throwable android.os.ParcelableException.getCause()' on a null object reference

android.telephony.TelephonyManager$1.lambda$onError$2
android.telephony.TelephonyManager$1.lambda$onError$2 (TelephonyManager.java:5346)
android.telephony.-$$Lambda$TelephonyManager$1$DUDjwoHWG36BPTvbfvZqnIO3Y88.run (-.java:6)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1167)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:641)
java.lang.Thread.run (Thread.java:919)

No Reference what part of app causing this but I am using TelephonyManager to get device MCC and MNC as below

  fun getMCCFromDevice(context: Context): String? {
    try {
        val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        if (tm != null) {
            val networkOperator = tm!!.getNetworkOperator()
            if (!TextUtils.isEmpty(networkOperator)) {
                return networkOperator.substring(0, 3)
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }

    return null

}

fun getMNCFromDevice(context: Context): String? {
    try {
        val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        if (tm != null) {
            val networkOperator = tm!!.getNetworkOperator()
            if (!TextUtils.isEmpty(networkOperator)) {
                return networkOperator.substring(3) // Extract MNC part
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }

    return null
}

Upvotes: 7

Views: 1259

Answers (1)

bonnyz
bonnyz

Reputation: 13558

If the issue is due to accessing the java.lang.Throwable android.os.ParcelableException.getCause() method on this exception instance, you should probably avoid calling e.printStackTrace() in your catch block, since internally it will invoke the getCause() method.

Upvotes: 2

Related Questions