[英]Java swing , Error Retrieving Image From Database using Eclipse
错误:Show $ 2.actionPerformed(Show.java:79)处javax.swing.ImageIcon中的java.lang.NullPointerException。(未知源)
我认为问题出在BufferedImage
,是否还有其他选择来检索来自Database的image(Blob)。 我正在使用Sqlite数据库。
代码如下:
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);
}
}
看起来好像无法识别图像的数据格式( 这是ImageIO唯一返回null的时间 )。 但是,由于图像是受支持的格式 (PNG),因此这非常奇怪。 我能够在Oracle错误跟踪器上找到PNG加载错误 ,但是该错误在2006年被标记为已解决。
我最好的建议是尝试使用第三方图像库,例如Commons Imaging , Sixlegs或许多其他数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.