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

6

77

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.

Langue answered 26/10, 2012 at 4:2 Comment(0)
P
74

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.

Pew answered 19/4, 2013 at 15:43 Comment(4)
How lame that we should always check for null because of something that seems to happen very rarely (case 3).Petit
In my case the link was contnet://XXX instead of content://XXX, dammitHindemith
@Pew can you help me out too on my latest question?Tabanid
@MickeyTin Whoa. It took me a minute to find the difference between them. You better use SCHEME_CONTENT from next time XD.Flunkey
L
12

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.

Lunular answered 26/10, 2012 at 5:14 Comment(0)
A
2

ContentResolver.query() will return null in the following cases:

  1. If you try to pass column names which don't exist in the database (a very common case is when developers use constants as column names, because they look similar to columns).

  2. It is likely to be null because your URI argument is invalid.

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

Acadian answered 26/10, 2012 at 5:15 Comment(0)
I
1

I had the same problem. My bug was to not close a Cursor for the Provider so a later query call lead to null.

Installment answered 25/2, 2016 at 16:21 Comment(2)
This is the one. If you don't close your cursor, the queries that follow might or might not work (and eventually will return the URI as null). I sat on this for an hour or so, then saw this. +1Ibidem
How following query will return null?Methedrine
M
0

If you forget to declare the provider in manifest your queries might return null.

Magdalenamagdalene answered 9/5, 2015 at 17:56 Comment(0)
G
-4

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

Guarani answered 26/10, 2012 at 4:29 Comment(3)
I thought I'd gotten empty cursor objects back before but I could be mistaken...have you seen this written in any semi-official place?Langue
Nope. I used to have test data always.Guarani
It does not return "null" if there is no result. It returns just an empty cursor.Beefwood

© 2022 - 2024 — McMap. All rights reserved.