Does anyone have idea how to solve this error : "o...
# android
j
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
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)
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
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
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)