I am just trying to Convert a blob string which is in Database to Byte array and then after converting it convert to buffered Image and then assign it to a label Here's my code
package ims.project;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import javax.imageio.ImageIO;
public class readingdata extends JFrame {
readingdata() {
JPanel pane = new JPanel();
JLabel label1 = new JLabel("help");
JLabel label2 = new JLabel("33");
pane.add(label1);
pane.add(label2);
setVisible(true);
add(pane);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ims1", "root", "root");
Statement st = con.createStatement();
String ss = "select Pic from supplier where Supplier_id= '" + label2.getText() + "'";
JOptionPane.showMessageDialog(null, label2.getText());
ResultSet rs = st.executeQuery(ss);
while (rs.next()) {
Blob blob = rs.getBlob("Pic");
int blobLength = (int) blob.length();
byte[] blobAsBytes = blob.getBytes(1, blobLength);
final BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(blobAsBytes));
label2.setIcon(new ImageIcon(bufferedImage));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main(String args[]) {
new readingdata();
}
}
but when i run this code it's show the Null pointer Stack-trace
java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:228)
at ims.project.readingdata.<init>(readingdata.java:47)
at ims.project.readingdata.main(readingdata.java:60)
Please try the following piece of code: