'Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Cursor?

And in IntelliJ the function onActivityResult is crossed out..

I'm trying to use cursor.getColumnIndex in the video Ive seen it works but here says Ive to initialize cursor.

What about these lines:

val cursor = contentResolver.query(selectedImage,filePathColum,null,null,null) 

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PICK_IMAGE_CODE  && data!=null){
            val selectedImage=data.data
            val filePathColumn=arrayOf(MediaStore.Images.Media.DATA)
            val cursor = contentResolver.query(selectedImage!!,filePathColumn,null,null,null)
           //cant use it here
            cursor.moveToFirst()
        }
        //and here                
        val columnIndex= cursor.getColumnIndex(filePathColumn[0])
    }
}


Solution 1:[1]

You can just either add 2 exclamation marks to make it non-null asserted or wrap it with a let call.

cursor!!.moveToFirst()

or

cursor?.moveToFirst()

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 srijan pandey