Jinal Barot

07/15/2019, 6:42 AM
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


07/15/2019, 6:44 AM
Cursor can be null, you can see it in
method signature, it annotated with
Just check for cursor nullability in your condition and Kotlin smart cast will handle it for you:
if (cursor != null && cursor.count > 0)
Also, a side note, no need to use
, you can see that IDE suggest you to use
, 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: Also, #getting-started is good channel if you just started and have questions about basic language features

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)