Question

I always see people say that it cursor is never null so you dont need to check for null or not but then there are other people that say that cursor can be null and you chould always check it.

so looking at the query method here it does show that null gets returned so I dont know if I am just missing something or are the people saying that cursor is never null wrong?

do you or do you not need to check if Cursor is null when a query is made?

Was it helpful?

Solution

For ContentResolver queries, yes, the returned Cursor can be null as illustrated by the code you linked.

For SQLiteDatabase queries, no, checking for null Cursor is not necessary. Though it does not hurt much.

OTHER TIPS

SQliteDatabase.query() returns a Cursor object, which is positioned before the first entry. Cursor is never null but it can be empty.

For ContentResolver queries it can be null. Check this SO question.

Be safe and make your code solid, check for null. You can refer to this code from Android. As we can see it is possible to get null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top