簡體   English   中英

Java swing,使用Eclipse從數據庫檢索圖像時出錯

[英]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 ImagingSixlegs或許多其他數據庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM