'NullPointerException: NfcAdapter.getDefaultAdapter(this) must not be null - Kotlin

I developed an application to get NFC adapter using NfcAdapter.getDefaultAdapter(this) function. But it gives NullPointerException - NfcAdapter.getDefaultAdapter(this) must not be null.

class TappingActivity : BaseActivity() {

    private lateinit var mNfcAdapter: NfcAdapter
    private lateinit var mFusedLocationClient: FusedLocationProviderClient
    private lateinit var mBroadcastReceiver: BroadcastReceiver
    private lateinit var mAdapter: RecyclerViewAdapter<SecurityPoint, ViewHolder>

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tapping)
        ButterKnife.bind(this)
        setSupportActionBar(toolbar)
        savedInstanceState?.let {
            lastLoggedPremisesID = it.getInt("lastLoggedPremisesID")
        }

        mNfcAdapter = NfcAdapter.getDefaultAdapter(this)
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)

        if (!mNfcAdapter.isEnabled) {
            snack(getString(R.string.str_nfc_disabled), Snackbar.LENGTH_INDEFINITE, R.color.error)
        }

}
override fun onResume() {
        super.onResume()
        setupForegroundDispatch()
    }

    override fun onPause() {
        super.onPause()
        stopForegroundDispatch()
    }

    override fun onDestroy() {
        super.onDestroy()
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver)
    }

    private fun setupForegroundDispatch() {
        val intent = Intent(this, TagDetection::class.java)
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
        val intentFilter = IntentFilter()
        intentFilter.addAction(NfcAdapter.ACTION_TAG_DISCOVERED)
        intentFilter.addCategory(Intent.CATEGORY_DEFAULT)
        val techList = arrayOf<Array<String>>()
        mNfcAdapter.enableForegroundDispatch(this, pendingIntent, arrayOf(intentFilter), techList)
    }

    private fun stopForegroundDispatch() {
        mNfcAdapter.disableForegroundDispatch(this)
    }

}


E/AndroidRuntime: FATAL EXCEPTION: main Process: com.antlergroup.patrolsystem, PID: 11333 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.antlergroup.patrolsystem/com.antlergroup.patrolsystem.ui.SecurityPointRegistrationActivity}: java.lang.NullPointerException: NfcAdapter.getDefaultAdapter(this) must not be null at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3897) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4076) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2473) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:219) at android.app.ActivityThread.main(ActivityThread.java:8349) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055) Caused by: java.lang.NullPointerException: NfcAdapter.getDefaultAdapter(this) must not be null at com.antlergroup.patrolsystem.ui.SecurityPointRegistrationActivity.onCreate(SecurityPointRegistrationActivity.kt:72) at android.app.Activity.performCreate(Activity.java:8085) at android.app.Activity.performCreate(Activity.java:8073) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3870)




Solution 1:[1]

You need to define NfcManager first then you can call the getDefaultAdapter method. Please refer to the code snippet.

 private var mNfcAdapter: NfcAdapter? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tapping)

        NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
        mNfcAdapter = manager.getDefaultAdapter();
}

For more information, you can refer to this link

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1