Reputation: 25
how to update image showing in imageView i set this image from database code to get image on imageView from db is...
InputStream is = resultSet.getBinaryStream("image");
OutputStream os;
os = new FileOutputStream(new File("src/sources/images/photo.jpg"));
byte[]content = new byte[1024];
int size = 0;
while((size=is.read(content))!= -1)
{
os.write(content,0,size);
}
os.close();
is.close();
image = new Image("file:src/sources/images/photo.jpg");
imageView.setImage(image);
image is saving in BLOB type in MySQL database
Now i want to update this image and set it again this image if i dont choose any image using file chooser please solve my this problem thanks in advance
File chooser code
stage = (Stage) showScene.getScene().getWindow();
file = fileChooser.showOpenDialog(stage);
if(file != null){
image = new Image(file.getAbsoluteFile().toURI().toString(),imageView.getFitWidth(),imageView.getFitHeight(),true,true);
imageView.setImage(image);
imageView.setPreserveRatio(true);
}
fis = new FileInputStream(file); // here i got error(null pointer exception) if i try to update withouting choosing image from filechooser
I pass fis to the preparedstatement to update and also insert
Upvotes: 1
Views: 516
Reputation: 2293
You are getting a NullPointerException
because if you don't choose a file with the FileChooser, your file
variable is set to null. This can be resolved by altering your if statement a little.
file = fileChooser.showOpenDialog(stage);
if (file == null) {
file = new File("path/to/default/file")
}
image = new Image(file.getAbsoluteFile().toURI().toString(),imageView.getFitWidth(),imageView.getFitHeight(),true,true);
imageView.setImage(image);
imageView.setPreserveRatio(true);
fis = new FileInputStream(file);
Upvotes: 1