-1

Here there is such an error.

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: 
    Width (0) and height (0) must be non-zero

How to fix it?

ResultSet resultSet = connection.query("select url_image from "+name+" where id = "+List.get(i));
java.sql.Blob blob = null;

try {
    while (resultSet.next()) {
        blob = resultSet.getBlob("url_image");
    }
} catch (SQLException e4) {
    e4.printStackTrace();
}
BufferedImage destImage = null;
try {
    destImage = ImageIO.read(blob.getBinaryStream());
} catch (IOException e1) {
    e1.printStackTrace();
} catch (SQLException e1) {
    e1.printStackTrace();
}
Image scaledImage = destImage.getScaledInstance(photoLabel.getWidth(),photoLabel.getHeight(), Image.SCALE_DEFAULT); // error
ImageIcon imgIc = new ImageIcon(scaledImage);
photoLabel.setIcon(imgIc);

 photoLabel = new javax.swing.JLabel();
 .addComponent(photoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(photoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
private javax.swing.JLabel photoLabel;
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Is this code in the correct order? You appear to instantiate `photoLabel` after you're using it? It should have thrown a `NullPointerException` when you called `.getWidth()`. Also what does `getWidth` and `getHeight` return, my guess is that it returns `0` which is why you're getting that error. – Mark Dec 06 '18 at 10:54
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) BTW - *if a label has no text or icon, its preferred width & height **will be** 0.* – Andrew Thompson Dec 06 '18 at 12:58

1 Answers1

0

This code may give you proper output.

 ResultSet resultSet = connection.query("select url_image from "+name+" where id = "+List.get(i));
                    java.sql.Blob blob = null;

                    try {
                        while (resultSet.next()) {
                            blob = resultSet.getBlob("url_image");
                        }
                    } catch (SQLException e4) {
                        e4.printStackTrace();
                    }
                    BufferedImage destImage = null;
                    try {
                        destImage = ImageIO.read(blob.getBinaryStream());
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    } catch (SQLException e1) {
                        e1.printStackTrace();
                    }
                    //Image scaledImage = destImage.getScaledInstance(photoLabel.getWidth(),photoLabel.getHeight(), Image.SCALE_DEFAULT); // error
                    //ImageIcon imgIc = new ImageIcon(scaledImage);
                    //photoLabel.setIcon(imgIc);
                    photoLabel.setIcon(new ImageIcon(destImage ));


     photoLabel = new javax.swing.JLabel();
     .addComponent(photoLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(photoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    private javax.swing.JLabel photoLabel;
Java-Dev
  • 438
  • 4
  • 20