Insert blob image in MySQL - NullPointerException

I am working on a database application in which I embed images in a database. I store InputStreams in the database as a BLOB, and I am having problems retrieving them and installing them in ImageIcon.

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"); } } 

Photos is an ArrayList using my Photo class, which I return. Every time I try to display images, I get the following error ...

 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at business.appdev.Home.paintEditFrame(Home.java:577) 

which comes from the following code.

 editImageLabel[i].setIcon( new ImageIcon(img.getScaledInstance(imageSize, imageSize, imageSize))); 

I have some basic println commands that show me what is returned from a MySQL database, which

 java.io.ByteArrayInputStream@2c0213a5 Retrieving a photo java.io.ByteArrayInputStream@252ab7be Retrieving a photo 

where img is BufferedImage. Any help would be greatly appreciated, thanks!

Code to handle the InputStream returned from the result set

 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))); 

updated photo class with byte [] data Based on some other posts I read, I store bytes [] in varbinary in MySQL. After that I take photo data from my database using

 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(); 

Then I create my photo object and return an ArrayList of photos. This threw a NullPointerException, but now I can not get ImageIcon JLabels to appear. I use the following code to add them to 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))); 

and then place the label on the JPanel.

+5
source share
1 answer

Avoid problems with encoding and decoding images by storing the image in the file system and only the path to the image in your database. This can help reduce the size of the database, which in turn can help your transactional efficiency. You can write a quick code to make a numerical file name for your server.

0
source

All Articles