What makes Android ContentResolver.query () return null?

Under what conditions does ContentResolver.query () return null instead of a cursor object? I got empty cursors, but only realized that the method can also return null. I was not able to track the circumstances in which this occurs.

+42
android
Oct 26
source share
6 answers

I just stumbled upon the same issue due to the user crash message I received today for my application. If the Android documentation is unclear about something, it helps to look at the source code. Here is what I found about the reasons for returning ContentResolver.query() null :

  • Content provider cannot be purchased. This may be due to a problem with the specified Uri or because it simply does not exist on the system. If Uri is a problem, the reasons are as follows: the protocol is not content:// , or Uri does not have part of the authorization string (Uri.getAuthority () == null).

  • The request method received by the provider itself returns null .

  • A content provider might have been purchased, but a RemoteException was thrown during the request.

Especially because of (2.) it is rather arbitrary, which may be the reason for null as a result, since there are no rules. But usually, if SQLite is the back-end of the ContentProvider , you can expect at least some kind of empty Cursor instead of a null result.

The ContentProvider Android system performs some checks before they return anything. If the input is not as expected, the likelihood that null can be returned is unlikely. But honestly, this has never happened to me before. I usually get an IllegalArgumentException in case of problems with the request parameters. Perhaps some ContentProvider implementations return null in the case of empty result sets.

Anyway. It seems necessary to always check for null . Especially, for example, the reason number (3.) is what can happen on any Android device.

+54
Apr 19 '13 at 15:43
source share

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

+5
Oct 26 '12 at 5:14
source share

ContentResolver.query () returns null in the following cases:

  • If you try to pass column names that are not in the database (a very common case is when developers use constants as column names because they are similar to columns).

  • It will probably be null, because your URI argument is not valid.

There may be other cases in which it returns null. However, the above two cases are very common reasons why developers pull their hair :)

+3
Oct 26 '12 at 5:15
source share

If you forget to declare the provider in the manifest, your requests may return null.

+1
May 09 '15 at 17:56
source share

I had the same problem. My mistake was not to close the cursor for the provider, so that a later request would call zero.

+1
Feb 25 '16 at 16:21
source share

If there is no result, it returns null. I want to say that if a given database query gives nothing (not even a single row of data), then query () returns null.

-2
Oct 26
source share



All Articles