This question already has answers here:
What is a NullPointerException, and how do I fix it?
                                
                                    (12个答案)
                                
                        
                                2年前关闭。
            
                    
我试图在jtextpane中显示来自mysql的图像,但不起作用,当我尝试将输入流转换为BufferedImage时出现问题,您能帮我吗?

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 508, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JTextPane textPane = new JTextPane();
    textPane.setEditable(false);
    textPane.setBounds(55, 40, 370, 192);
    frame.getContentPane().add(textPane);

    try{
        Statement a = (Statement) con.createStatement();
        ResultSet b = a.executeQuery("Select imagen from chat");
        InputStream i = null;
        ImageIcon ii = null;
        if (b.next()) {
            i = b.getBinaryStream("imagen");
            BufferedImage bi = ImageIO.read(i);
            ii = new ImageIcon(bi);
            textPane.insertIcon(ii);
        }
    } catch ( SQLException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


控制台说...

java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at imagen.initialize(imagen.java:75)


我找不到解决方案

最佳答案

如果imagen.java文件的第75行上的元素可能为null,则会收到NullPointerException错误。 Go to solutuon

关于java - 我怎样才能从mysql获得blob并显示在jtextpane中? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44631047/

10-10 09:06