I want to load an image for each different shoe and I'm using JLabel to help me do this.
I put the image's path in the JLabel constructor but when the code runs, it doesn't display any image. Instead of an image, the image path is written.

appreciate any help and suggestion

package badmintonstore;

/**
 *
 * @author TaimooR
 */

import java.awt.GridLayout;
import javax.swing.*;

/*
 * The class displays shoes
 */
public class Shoes extends JPanel
{
    //Class Fields
    private JLabel icon;
    private JLabel price;
    private JTextField qtyInput;
    private double priceS = 0.0;


    public Shoes (String showPrice,String path)
    {
            setLayout(new GridLayout(2,2));

            // Constructing shoes
            icon = new JLabel(path);
            price = new JLabel(showPrice);
            qtyInput = new JTextField(2);
            priceS = Double.valueOf(showPrice);
            add(icon);
            add(price);
            add(qtyInput);
    }
    /*
     * Method getShowPrice
     * Return the price of the shows
     */
    public double getShoePrice()
    {
        double finalShoePrice;
        double quantity = Double.valueOf(qtyInput.getText());
        finalShoePrice = quantity*priceS;
        return finalShoePrice;
    }
}

Dani AI

Generated

A quick diagnosis and practical checklist.

A JLabel constructed with a plain string will render that string, so an Icon must be supplied to show an image. The NullPointerException seen after switching to a classloader lookup happens because a filesystem path (the C:... string passed from ) was handed to getResource; getResource searches the classpath and returns null for files on disk. Creating an image from a null URL triggers the NPE. This is the difference was getting at: classpath resources and filesystem files are handled differently.

How to troubleshoot and fix (stepwise, no code copied from replies):

  • Decide whether the images are packaged with the app or live on the user machine. Packaged resources must be placed under the source tree so the build copies them into classes/jar; classloader lookups expect a classpath-style path (use an absolute resource path starting at the classpath root). Disk files must be loaded directly from the filesystem.
  • Verify what the classloader returns and whether the file exists: print the result of getResource(...) (it should not be null) or check the file existence on disk. ImageIO-based loading will throw an IOException on failure and is useful for diagnosing load problems.
  • Remember Java string literals need escaped backslashes for Windows paths; also ensure file permissions and correct path spelling.

Additional notes: construct and show Swing UI on the Event Dispatch Thread (invokeLater), call pack() after components are added, and be mindful of layout sizing (GridLayout will size cells to content). For large images, scale them before display or load them off the EDT (SwingWorker). If the NPE still appears, include the stack trace and the printed getResource/File.exists outputs to narrow the exact failing line.

Recommended Answers

All 4 Replies

Member Avatar for Member #887084
icon = new JLabel(path);

For images with JLabel, you need to pass in an Icon. See public JLabel(Icon image) for more information.

So, basically something like this:

icon = new JLabel(new ImageIcon(getClass().getResource(path)));

Make sure you have the images or image folder in your build path.

@ztini I tried what you suggested but I get a NullPointerException. Here is my main class. thank you in advance

public BadmintonStore()
    {
        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Shoes shoe = new Shoes ("199.99","C:\\Users\\TaimooR\\Desktop\\iPhoneS\\fifa11.jpg");
        add(shoe,BorderLayout.CENTER);
        pack();
        setVisible(true);
    }
    public static void main(String[] args)
    {
        BadmintonStore Montreal = new BadmintonStore();
    }

}

Here is the Updated Shoe class

package badmintonstore;

/**
 *
 * @author TaimooR
 */

import java.awt.GridLayout;
import javax.swing.*;

/*
 * The class displays shoes
 */
public class Shoes extends JPanel
{
    //Class Fields
    private JLabel icon;
    private JLabel price;
    private JTextField qtyInput;
    private double priceS = 0.0;


    public Shoes (String showPrice,String path)
    {
            setLayout(new GridLayout(2,2));

            // Constructing shoes
            icon = new JLabel(new ImageIcon(getClass().getResource(path)));
            price = new JLabel(showPrice);
            qtyInput = new JTextField(2);
            priceS = Double.valueOf(showPrice);
            add(icon);
            add(price);
            add(qtyInput);
    }
    /*
     * Method getShowPrice
     * Return the price of the shows
     */
    public double getShoePrice()
    {
        double finalShoePrice;
        double quantity = Double.valueOf(qtyInput.getText());
        finalShoePrice = quantity*priceS;
        return finalShoePrice;
    }
}
Member Avatar for Member #887084

The example I gave you is for relative file location. That is, your call should look something like this: new Shoes(199.99, "images/shoe.jpg");

This requires the 'images' folder to be here:
C:\....\src\badmintonstore\images\shoe.jpg

if you don't want to use relative locations, just do:
icon = new JLabel(new ImageIcon(path));

They can be a pain to figure out at first, but you will be thankful you figured out relative paths later.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.