Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7999727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:35:11+00:00 2026-06-04T15:35:11+00:00

I am trying to write a code that displays an image selected using a

  • 0

I am trying to write a code that displays an image selected using a JFileChooser into another JFrame .I tried the following code below but only got 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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:35:12+00:00Added an answer on June 4, 2026 at 3:35 pm

    The value of img will only have a real value after the user click the button and chooses the file to display. Until this time, the value of img is null, so when it continues through your method and calls the line ImageIcon icon=new ImageIcon(img);, it is trying to create an ImageIcon object for null.

    To correct this, you should only be creating the ImageIcon when the user has chosen the file. Here is a change that should be closer to working correctly. (see the comments //ADDED and //REMOVED in the code below to see the changes…

    ...
    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();
            ...
    

    To explain what I’ve changed…

    1. The label will now be created as an empty JLabel when you first start the program. It is also stored as a global variable so we can access it later
    2. When the button is clicked, the img is created, as before, and then it is loaded into your label using setIcon();
    3. The label is resized, then revalidate() and repaint() to make sure that the image is drawn after it is set.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write code that will load an image from a resource, and
I'm trying to write a code that visits in another website The other page
I'm trying to write some code that displays my player's X value in text.
I'm trying to write code that returns the depth of the deepest leaf in
I've been trying to write code that loads a .png file, attaches hotspot information,
I am trying to write some code that allows SVG elements to be dragged
I am trying to write a code that can get the names of the
I am trying to write some code that that will draw the line which
I am trying to write a C code that will find hyperlinks in a
I'm trying to write some c++ code that tests if a string is in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.