I using java.sql.Blob object to save images in mySql. When i try insert object in database i have exception "Java.lang.NullPointerException"
Here is my code :
@Override
protected Object doInBackground(Object... arg0) {
try {
Bitmap photo = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Users user = new Users();
user.setName("haris");
Next code get me exception.
try {
Blob blo = null;
blo.setBytes(1, getBytes(photo));
} catch (Exception e) {
System.out.println("Error:"+e.toString()); //There is my Exception
}
/
if (Korisnik_servis.Registracija(user)){ // Call my REST services
System.out.println("Registration succesful");
}
else
{
System.out.println("Registration not succesful");
}
} catch (Exception e) {
System.out.println("Error:"+e.toString());
}
return null;
}
Function for convert Bitmap to byte []
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
My question is: how to successfully initialize Blob object.