开发者

Avoiding IllegalArgumentException when using NULL as placeholder value in query?

I'm trying to get contacts photo from content providers and facing some kind of issue with this query.

Cursor p开发者_高级运维hoto = cr.query( 
                   PicURI,  
                   new String[] {
                      CommonDataKinds.Photo.PHOTO
                   }, 
                   Data._ID + "=?",  
                   new String[] { photoId }, 
                   null);

This is the related line from logcat:

java.lang.IllegalArgumentException: the bind value at index 1 is null

After some tests,i figured out that I get this exception if one of the contacts have no picture set.

I searched and found this same problem. Seems like it's a bug. And I can't solve it. Any help will be appreciate.


You certainly don't want to find photos with id=null, so you have to use this code:

if (photoId != null) {
   //your code here
}

Would be nice, if you posted full stack trace.


You're correct - it is the same problem as this:

http://code.google.com/p/android/issues/detail?id=7636

You're also correct about it's being a bug.

The "bug" is in your code. You need to check your arguments for "nulls", and you should NOT do a query or an update if any of your input parameters are null!

Two choices:

if (myphoto[i] == null)
  query.bindNull(i)

... or ...

if (myphoto[i] != null)
  query ()

It's YOUR responsibility to validate and clean up YOUR data before calling an API...

IMHO...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜