Tôi đang làm việc trên một ứng dụng cơ sở dữ liệu trong đó tôi chèn hình ảnh vào cơ sở dữ liệu. Tôi đang lưu trữ InputStreams trong cơ sở dữ liệu dưới dạng BLOB và tôi gặp sự cố khi truy xuất chúng và đặt chúng thành ImageIcon.Chèn một hình ảnh blob vào MySQL - NullPointerException
try{
// Return a resultset That contains
// the photos from the hashtags the user is following.
preparedStatement = conn.prepareStatement("SELECT * FROM PHOTOS WHERE CARID=?");
preparedStatement.setInt(1, 1);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
newPhoto = new Photo(resultSet.getInt(1), resultSet.getInt(2),
resultSet.getLong(3), resultSet.getBinaryStream(4));
System.out.println(resultSet.getBinaryStream(4));
photos.add(newPhoto);
System.out.println("Retrieving a photo");
}
}
ảnh là một ArrayList sử dụng lớp ảnh của tôi mà tôi quay trở lại. Mỗi lần tôi cố gắng hiển thị hình ảnh, tôi nhận được lỗi sau ...
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at business.appdev.Home.paintEditFrame(Home.java:577)
được đến từ các mã sau ..
editImageLabel[i].setIcon(
new ImageIcon(img.getScaledInstance(imageSize, imageSize, imageSize)));
Tôi đã một số lệnh println cơ bản đang hiển thị cho tôi những gì đang quay trở lại từ cơ sở dữ liệu MySQL, là
[email protected]
Retrieving a photo
[email protected]
Retrieving a photo
nơi img là một BufferedImage. Mọi sự trợ giúp đều rất được trân trọng, xin cảm ơn!
Mã để xử lý của InputStream trở về từ ResultSet
for(int i = 0; i < photos.size(); i++){
img = ImageIO.read(photos.get(i).getInputStream());
editImageLabel[i] = new JLabel("");
editImageLabel[i].setIcon(new ImageIcon(img.getScaledInstance(imageSize, imageSize, imageSize)));
cập nhật lớp ảnh với byte [] data Dựa trên một số bài viết khác mà tôi đã đọc, tôi đang lưu trữ các byte [] vào một varbinary trong MySQL. Sau đó, tôi lấy dữ liệu ảnh từ cơ sở dữ liệu của mình bằng cách sử dụng
InputStream in = resultSet.getBinaryStream(4);
byte[] photoData = new byte[(int) resultSet.getLong(3)];
ByteArrayOutputStream os = new ByteArrayOutputStream();
for (int len; (len = in.read(photoData)) != -1;)
os.write(photoData, 0, len);
os.flush();
Sau đó tôi tạo đối tượng ảnh của mình và trả về một ArrayList ảnh. Điều này đã loại bỏ NullPointerException, nhưng bây giờ tôi không thể nhận được các JLabels ImageIcon để hiển thị. Tôi sử dụng mã sau để thêm chúng vào JPanel,
InputStream in = new ByteArrayInputStream(photos.get(i).getData());
BufferedImage newImage = ImageIO.read(in);
editImageLabel[i] = new JLabel("");
editImageLabel[i].setIcon(new ImageIcon(newImage.getScaledInstance(imageSize, imageSize, Image.SCALE_DEFAULT)));
và sau đó tôi đặt nhãn lên JPanel.
bạn có thể kiểm tra xem 'resultSet.getBinaryStream (index)' có đang truy xuất bạn bất kỳ dữ liệu nào không? –
Bạn có thể chia sẻ mã xử lý luồng trong lớp 'Ảnh' của mình không? –
@ AlexandroSifuentesDíaz phần mã cuối cùng là những gì trở về từ resultSet.getBinaryStream (index). Tôi đã có nó trong lệnh println chỉ cho mục đích thử nghiệm. –