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

}
5
  • 1) See What is a stack trace, and how can I use it to debug my application errors? & What is a Null Pointer Exception, and how do I fix it? 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 along with layout padding and borders for .. Commented Jul 31, 2015 at 17:48
  • .. for white space. 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. Commented Jul 31, 2015 at 17:49
  • Please post the entire stack trace, along with a minimal reproducible example (with imports etc.) so that we can match up the line numbers to the source. Commented Jul 31, 2015 at 17:55
  • I am new to stackoverflow ! sorry for that ! Commented Jul 31, 2015 at 18:03
  • An edit adding the details I suggested is better than 'sorry'. ;) Commented Jul 31, 2015 at 19:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Formats are PNG , so ImageIO should recognize it . is there any other way to retrieve the Blob(Image) from database ?
@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).
Still , no solutions . I searched in a lot of sites , but nothing . if you find any please do let me know .
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 or maybe Sixlegs).
I tried with Different PNG s in fact i tried it with JPG , But Same problem Occurs
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.