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?
Baca Juga
- Saving Image Clicked From Camera Or Loaded From Gallery In Database And Retrieving It
- Opencontactphotoinputstream Causes “java.lang.illegalstateexception: Get Field Slot From Row 0 Col 0 Failed”
- Bind The Progress Of A Query To A Progressproperty Of A Progressbar For Informing The User Of Download And Upload Time In Javafx?
Post a Comment for "Java.lang.nullpointerexception In Blob"