Java.lang.nullpointerexception In Blob
I'm using java.sql.Blob type for images in MySql database. When I try insert object in database I get exception 'Java.lang.NullPointerException' @Override protected Object doInBack
Solution 1:
I am using this piece of code to store bitmaps in blobs (converted from drawable):
db = this.getWDB();
ContentValuesvalues=newContentValues();
Drawabled= model.getAppIcon();
BitmapDrawablebitDw= ((BitmapDrawable) d);
Bitmapbitmap= bitDw.getBitmap();
ByteArrayOutputStreamstream=newByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageInByte = stream.toByteArray();
values.put(COLUMN_ICON_BLOB, imageInByte);
db.insert(TABLE_APPS, null, values);
db.close();
and to retrieve it from database this method is used
publicstatic Drawable convertByteArrayToDrawable( byte[] byteArrayToBeCOnvertedIntoBitMap) {
BitmapbitMapImage= BitmapFactory.decodeByteArray(
byteArrayToBeCOnvertedIntoBitMap, 0,
byteArrayToBeCOnvertedIntoBitMap.length);
returnnewBitmapDrawable(bitMapImage);
}
Solution 2:
You have exception because Blob is an interface and setBytes is an abstract method. Check this link how to store Image as blob in Sqlite & how to retrieve it?
Post a Comment for "Java.lang.nullpointerexception In Blob"