Reputation: 197
Error: java.lang.NullPointerException at javax.swing.ImageIcon.(Unknown Source) at Show$2.actionPerformed(Show.java:79)
I think the problem is occuring in BufferedImage
, is there any other option to retrieve image(Blob) form Database . I am using Sqlite database .
Here Goes The Code :
public class Show extends JFrame {
private JPanel contentPane;
private JTextField id;
BufferedImage bufImg = null;
JLabel img=null;
InputStream in=null;
ImageIcon imgs=null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Show frame = new Show();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
Connection con=null;
public Show() {
con=dB.Connect();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 588, 432);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
id = new JTextField();
id.setBounds(158, 23, 86, 20);
contentPane.add(id);
id.setColumns(10);
JButton show = new JButton("New button");
show.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
//String iid=null;
//iid="10";
//iid=id.getText().toString();
String q="select image from image where id='"+id.getText()+"'";
PreparedStatement ps=con.prepareStatement(q);
//ps.setString(1, id.getText().trim());
//ps.executeQuery();
ResultSet rs=ps.executeQuery();
while (rs.next()) {
in= rs.getBinaryStream("image");
bufImg = ImageIO.read(in);
img.setIcon(new ImageIcon(bufImg));// Console shows the error is in this line
}
rs.close();
ps.close();
}catch(Exception c)
{
c.printStackTrace();
}
}
});
show.setBounds(302, 22, 89, 23);
contentPane.add(show);
img = new JLabel("");
img.setBounds(151, 99, 325, 284);
contentPane.add(img);
}
}
Upvotes: 1
Views: 147
Reputation: 336
It appears as though the data format of the image is not recognized(This is the only time ImageIO returns null). However, as the image is a supported format (PNG), this is a very bizarre occurrence. I was able to find a PNG loading bug on the Oracle bug-tracker, however the bug was marked as resolved in 2006.
My best suggestion is to try using a 3rd party image library such as Commons Imaging, Sixlegs, or numerous others.
Upvotes: 1