Does anyone have idea how to solve this error : "only safe (?.) Or non-null asserted(!!.) Calls are allowed on a nullable receiver of type cursor?"
I'm trying to extract contacts using ContactsContract content provider by query which return response as a Cursor
g
gildor
07/15/2019, 6:44 AM
Cursor can be null, you can see it in
query
method signature, it annotated with
@Nullable
Just check for cursor nullability in your condition and Kotlin smart cast will handle it for you:
Copy code
if (cursor != null && cursor.count > 0)
gildor
07/15/2019, 6:49 AM
Also, a side note, no need to use
var
, you can see that IDE suggest you to use
val
, it will also help with smart cast in some cases
gildor
07/15/2019, 6:50 AM
Are you familiar with Kotlin nullale types? If not, I would recommend first check documentation:
https://kotlinlang.org/docs/reference/null-safety.html
Also, #getting-started is good channel if you just started and have questions about basic language features
l
Lou Morda
08/13/2019, 5:01 PM
i was a kotlin newbie not too long ago, double bang (!!) is always best to avoid, from what i've read. i got dinged pretty hard on an interview problem for using double bang (rightfully so i believe)