Question

I have a GUI which have texfields and button to it . If I click the next button the cursor should navigate in the database and set the row values into my textfields and put the path into the button to create image icon. But it gives me this error.

Code block:

if (event.getSource() == next) {
    try {   
        if (rs.next()) {    
            lastnameT.setText(rs.getString("lname"));
            firstnameT.setText(rs.getString("fname"));
            middlenameT.setText(rs.getString("mname"));
            aliasT.setText(rs.getString("alias"));
            sexT.setSelectedItem(rs.getString("sex"));
            ageT.setText(rs.getString("age"));
            civilstatusT.setSelectedItem(rs.getString("civilstatus"));
            addressT.setText(rs.getString("address"));
            religionT.setSelectedItem(rs.getString("religion"));
            monthT.setSelectedItem(rs.getString("m"));
            dayT.setSelectedItem(rs.getString("d"));
            yearT.setSelectedItem(rs.getString("y"));
            eaT.setSelectedItem(rs.getString("EA"));
            weightT.setText(rs.getString("weight"));;
            heightT.setText(rs.getString("height"));
            hairT.setSelectedItem(rs.getString("hair"));
            hairColorT.setSelectedItem(rs.getString("haircolor"));
            colorEyesT.setSelectedItem(rs.getString("colorofeyes"));
            complexionT.setSelectedItem(rs.getString("complexion"));
            gangT.setText(rs.getString("gang"));
            marksT.setText(rs.getString("marks"));
            preparedbyT.setText(rs.getString("preparedBy"));
            verifiedT.setText(rs.getString("verifiedBy"));

            //btnNewButton.putClientProperty(rs.getString("image"),rs.getString("image"));
            btnNewButton.setIcon(new ImageIcon(rs.getString("image")));
            //  save.setIcon(new ImageIcon(file.getPath()));
        }
        else {
            JOptionPane.showMessageDialog(null, "No more records");
        }
    }
    catch (Exception e) {
        JOptionPane.showMessageDialog(null,"Sa catch bumagsak");
        System.out.print(e);
    }
}

Error:

Uncaught error fetching image:
java.lang.NullPointerException
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at sun.awt.image.FileImageSource.getDecoder(Unknown Source)
    at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
    at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
    at sun.awt.image.ImageFetcher.run(Unknown Source)
Was it helpful?

Solution

It seems that rs.getString("image") is returning a non valid path to a file. You can easily check this:

//btnNewButton.setIcon(new ImageIcon(rs.getString("image")));
System.out.println(rs.getString("image"));

Some Tips

Database calls are time consuming tasks and may block the Event Dispatch Thread (a.k.a. EDT) causing the GUI become unresponsive. The EDT is a single and special thread where Swing components creation and update take place. To avoid block this thread consider use a SwingWorker to perform database calls in a background thread and update Swing components in the EDT. See more in Concurrency in Swing trail.

I'd suggest you take a look to the hints exposed in this answer. Summarized:

  • Wrap data in a domain class.
  • Use a SwingWorker to do the database calls in a background thread and update Swing components in the EDT.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top