Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes Android's ContentResolver.query() to return null?

Tags:

android

Under what conditions does ContentResolver.query() return null instead of a cursor object? I've gotten empty cursors before but just realized that the method can also return null. I haven't been able to trace the circumstances that this happens in, though.

like image 380
anyweez Avatar asked Oct 26 '12 04:10

anyweez


People also ask

What does ContentResolver query () return?

The ContentResolver. query() client method always returns a Cursor containing the columns specified by the query's projection for the rows that match the query's selection criteria. A Cursor object provides random read access to the rows and columns it contains.

What is the use of ContentResolver in Android?

The Content Resolver behaves exactly as its name implies: it accepts requests from clients, and resolves these requests by directing them to the content provider with a distinct authority. To do this, the Content Resolver stores a mapping from authorities to Content Providers.

What is ContentResolver in Kotlin?

The object of ContentResolver communicates with the provider object and the provider object receives the data request from the client and the requested actions are being performed and finally, the result is returned.


2 Answers

I just stumbled over the same problem due to a user crash report I received today for an app of mine. If the Android documentation is unclear about something it helps looking at the source code. This is what I found about the causes for ContentResolver.query() returning null:

  1. The content provider cannot be acquired. This can be due to a problem with the specified Uri or because it simply does not exist on the system. If the Uri is the problem the causes are: protocol is not content:// or the Uri does not have an authority string portion (Uri.getAuthority() == null).

  2. The acquired provider's query method itself returns null.

  3. The content provider could be acquired but a RemoteException was thrown during a query.

Especially because of (2.) it's pretty much arbitrary what might be the cause for null as a result since there are no rules defined. But usually, if SQLite is the back-end of a ContentProvider you can expect at least some empty Cursor object as a result instead of just null.

Android system ContentProviders do some checks though before they return anything. If the input is not as expected there's the unlikely chance that null may be returned. But to be honest, that never happened to me before. I usually get an IllegalArgumentException in case of query parameter problems. Maybe some ContentProvider implementations return null in case of empty result sets.

Either way. It seems to be necessary to always check for null. Especially reason number (3.) is probably something that can happen on any Android device.

like image 52
tiguchi Avatar answered Sep 21 '22 01:09

tiguchi


ContentResolver.query returns null if the uri scheme is not of the form content:// or if the contentProvider for the scheme itself does not exist.

like image 37
nandeesh Avatar answered Sep 23 '22 01:09

nandeesh