1

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);
}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
CodeHead
  • 177
  • 1
  • 2
  • 12
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for .. – Andrew Thompson Jul 31 '15 at 17:48
  • .. for [white space](http://stackoverflow.com/a/17874718/418556). 3) Don't add a tag for your IDE unless the question is about the IDE, or the errors only occurs when running in the IDE. – Andrew Thompson Jul 31 '15 at 17:49
  • Please post the entire stack trace, along with a [mcve] (with imports etc.) so that we can match up the line numbers to the source. – Andrew Thompson Jul 31 '15 at 17:55
  • I am new to stackoverflow ! sorry for that ! – CodeHead Jul 31 '15 at 18:03
  • An edit adding the details I suggested is better than 'sorry'. ;) – Andrew Thompson Jul 31 '15 at 19:05

1 Answers1

1

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.

  • Formats are PNG , so ImageIO should recognize it . is there any other way to retrieve the Blob(Image) from database ? – CodeHead Jul 31 '15 at 17:54
  • @CodeHead I'm looking into the details, but perhaps you should try to manually obtain the png image reader from its repective service provider (Service providers for image formats are registered in [IIORegistry](http://docs.oracle.com/javase/8/docs/api/javax/imageio/spi/IIORegistry.html)). – NitrogenReaction Jul 31 '15 at 18:07
  • Still , no solutions . I searched in a lot of sites , but nothing . if you find any please do let me know . – CodeHead Jul 31 '15 at 21:51
  • I've done some digging, and some sources say that the Java PNG loader does not fully support the format. However, the errors seem quite different. I suggest potentially trying to load several different PNGs to see if it only fails on that one. I suspect that the best solution is probably to look for an image library ([Commons Imaging](https://commons.apache.org/proper/commons-imaging/) or maybe [Sixlegs](https://code.google.com/p/javapng/)). – NitrogenReaction Aug 01 '15 at 04:26
  • I tried with Different PNG s in fact i tried it with JPG , But Same problem Occurs – CodeHead Aug 01 '15 at 05:57
  • Could you check that the data is being loaded/stored intact (save the raw data out to a file and see if you can open it)? – NitrogenReaction Aug 01 '15 at 06:05
  • I tried , and the raw data were successfully opened – CodeHead Aug 01 '15 at 07:28
  • That's extraordinarily bizarre. You could try having the program save the data to a file then open it as a temporary workaround. The best option in the long run is probably to try a third party library like the ones I mentioned. – NitrogenReaction Aug 01 '15 at 16:25