0

I am trying to put byte[] to JLabel on netbeans maven Project. I read some solutions and then implement it for me. However when I start application it gives me error lists such as;

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:228)
    at org.tutev.envanterys.gui.finger.FrmFinger.btnShowImagesActionPerformed(FrmFinger.java:104)
    at org.tutev.envanterys.gui.finger.FrmFinger.access$000(FrmFinger.java:29)
    at org.tutev.envanterys.gui.finger.FrmFinger$1.actionPerformed(FrmFinger.java:55)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)

Could you please kindly help me where is my mistake and how can I avoid this? Here is my code:

 try {
            Finger finger = new Finger();
            FingerService fingerService = new FingerService();

            finger = fingerService.getImage(Long.parseLong("1"));

            byte[] image1 = finger.getImage1();

            ByteArrayInputStream bais = new ByteArrayInputStream(image1);
            BufferedImage img = ImageIO.read(bais);

            lblFinger1.setIcon(new ImageIcon(img));

        } catch (IOException ex) {
            Logger.getLogger(FrmFinger.class.getName()).log(Level.SEVERE, null, ex);
        }

When I debug application I see that byte[] and bais has data but BufferedImage img is null.

goGud
  • 4,163
  • 11
  • 39
  • 63
  • I'll admit, I got to the part where you said "I am trying to put byte[] to JLabel.." and my mind screamed "WHY!?" – Neil Mar 01 '16 at 11:54

1 Answers1

0

You must check if your data really an image data.

As API documentation: ImageIO read method's explanation is;

Returns a BufferedImage as the result of decoding a supplied InputStream with an ImageReader chosen automatically from among those currently registered. The InputStream is wrapped in an ImageInputStream. If no registered ImageReader claims to be able to read the resulting stream, null is returned. The current cache settings from getUseCacheand getCacheDirectory will be used to control caching in the ImageInputStream that is created.

This method does not attempt to locate ImageReaders that can read directly from an InputStream; that may be accomplished using IIORegistry and ImageReaderSpi.

This method does not close the provided InputStream after the read operation has completed; it is the responsibility of the caller to close the stream, if desired.

Source

Yusuf K.
  • 4,195
  • 1
  • 33
  • 69