Loading an image using JFileChooser in a JFrame

I am trying to write code that displays an image selected using JFileChooser in another JFrame. I tried the following code below, but received only the following errors.

Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:228)
at power.<init>(fCGUI.java:53)
at fCGUI.main(fCGUI.java:11)

Here is the code:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class fCGUI
{
    public static void main(String []args)
    {
        power p=new power();
        p.setVisible(true);
    }
}

class power extends JFrame
{
    JFileChooser chooser;
    BufferedImage img;
    JButton button,button2;
    JFrame comp;
    String filename;
    File file ; 

    public power()
    {
        setSize(450,450);
        panel.setLayout(new BorderLayout());

        JPanel panel=new JPanel();
        getContentPane().add(panel);
        button =new JButton("press");

        panel.add(button,BorderLayout.NORTH);

        chooser = new JFileChooser();

        ActionListener action=new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (e.getSource()==button)
                {
                    chooser.showOpenDialog(null);
                    file = chooser.getSelectedFile();

                    try
                    {
                        img=ImageIO.read(file);
                    }
                    catch(IOException e1) {}
                }

                if (e.getSource()==button2)
                {
                    comp.setVisible(true);
                }
            }
        };

        ImageIcon icon=new ImageIcon(img);
        JLabel label=new JLabel(icon);

        JPanel secpanel=new JPanel();

        comp=new JFrame();
        comp.setSize(650,500);
        comp.setLayout(new BorderLayout());
        comp.setTitle("View Report");

        JRootPane compPane=comp.getRootPane();
        Container contePane=compPane.getContentPane();
        contePane.add(secpanel);

        secpanel.add(label,BorderLayout.CENTER);

        button2=new JButton("access");
        button2.addActionListener(action);
        button.addActionListener(action);

        panel.add(button2,BorderLayout.SOUTH);
    }
}
+3
source share
2 answers

The value imgwill have real value after the user presses the button and selects the file to display. Up to this point, the value imgis equal null, so when it continues its method and calls a string ImageIcon icon=new ImageIcon(img);, it tries to create an object ImageIconfor null.

, ImageIcon, . , . (. //ADDED //REMOVED , ...

...
class power extends JFrame {
    JFileChooser chooser;
    BufferedImage img;
    JButton button,button2;
    JFrame comp;
    String filename;
    File file ; 
    JLabel label; // ADDED

    public power() {
    ...
            public void actionPerformed(ActionEvent e) {
                if (e.getSource()==button) {
                    chooser.showOpenDialog(null);
                    file = chooser.getSelectedFile();

                    try {
                        img=ImageIO.read(file);
                        ImageIcon icon=new ImageIcon(img); // ADDED
                        label.setIcon(icon); // ADDED

                        Dimension imageSize = new Dimension(icon.getIconWidth(),icon.getIconHeight()); // ADDED
                        label.setPreferredSize(imageSize); // ADDED

                        label.revalidate(); // ADDED
                        label.repaint(); // ADDED
                    }
                    catch(IOException e1) {}
                }

                if (e.getSource()==button2){
                    comp.setVisible(true);
                }
            }
        };

        //ImageIcon icon=new ImageIcon(img); // REMOVED
        //JLabel label=new JLabel(icon); // REMOVED
        label = new JLabel(); // ADDED

        JPanel secpanel=new JPanel();
        ...

, ...

  • label JLabel . , .
  • img , , label setIcon();
  • , revalidate() repaint(), , , .
+3

ImageIO.read , JButton. , ActionListener. - img null, NullPointerException, ImageIcon icon=new ImageIcon(img).

+1

All Articles