Skip to content Skip to sidebar Skip to footer

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();
    ContentValues values = new ContentValues();

         Drawable d = model.getAppIcon();
    BitmapDrawable bitDw = ((BitmapDrawable) d);
    Bitmap bitmap = bitDw.getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    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

    public static Drawable convertByteArrayToDrawable( byte[] byteArrayToBeCOnvertedIntoBitMap) {

    Bitmap bitMapImage = BitmapFactory.decodeByteArray(
            byteArrayToBeCOnvertedIntoBitMap, 0,
            byteArrayToBeCOnvertedIntoBitMap.length);

    return new  BitmapDrawable(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"