0

Here's the error I am getting:

Using directory  file:/D:/CCSF%20Docs/CS111B/Java%20Projects/LabelDemo2.java/bin/ 
Image url is null 
Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source) 
    at LabelDemo2.<init>(LabelDemo2.java:23) 
    at LabelDemo2.main(LabelDemo2.java:46)

Could anyone tell me why I can't load the image? The java.jpg image is in D:/CCSF%20Docs/CS111B/Java%20Projects/LabelDemo2.java/bin/

I don't know why I am getting a null exception/error.

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

public class LabelDemo2 extends JFrame {
    private ImageIcon image1;
    private JLabel label1;
    private ImageIcon image2;
    private JLabel label2;
    private ImageIcon image3;
    private JLabel label3;
    private static final long serialVersionUID = 1L;

    LabelDemo2() {
        setLayout(new FlowLayout());

        System.out.println("Using directory " + getClass().getResource(""));         
        System.out.println("Image url is " + getClass().getResource("java.jpg"));         
        image1 = new ImageIcon(getClass().getResource("java.jpg"));
        label1 = new JLabel(image1);
        add(label1);


        label1 = new JLabel(image1);
        add(label1);

        image2 = new ImageIcon(getClass().getResource("java.jpg"));

        label2 = new JLabel(image2);
        add(label2);

        image3 = new ImageIcon(getClass().getResource("java.jpg"));

        label3 = new JLabel(image3);
        add(label3);
    }

    public static void main (String args[]) {
        LabelDemo2 gui = new LabelDemo2();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setVisible(true);
        gui.pack();
        gui.setTitle("Label Demo");
    }
}
Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
tekamts
  • 1
  • 1
  • Is the image in the source folder? – Bahramdun Adil Feb 16 '16 at 07:52
  • 1
    Possible duplicate of [Loading resources using getClass().getResource()](http://stackoverflow.com/questions/2343187/loading-resources-using-getclass-getresource) – DonyorM Feb 16 '16 at 08:03
  • `getResource()` has a somewhat interesting way of specifying paths. Seen the above link for more information about it. In short, it's easiest to put the file you want to read in your source folder/in your project and specify the path as an absolute path where "/" stands for the source folder. – DonyorM Feb 16 '16 at 08:04
  • can you try with `ClassPathResource fileReference = new ClassPathResource(filename);` then `fileReference.getInputStream()` – Braj Feb 16 '16 at 09:17

2 Answers2

0

If the image is in your source folder, then you do like below to read the Image

getClass().getResource("/java.jpg")

Just put / or \\

If it is outside of your source folder, then you can read it by InputStream with Absolute path

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
0

You can try

ClassLoader classLoader = this.getClass().getClassLoader();
InputStream in = classLoader.getResourceAsStream(path);

OR

ClassPathResource fileReference = new ClassPathResource(filename);  
InputStream in = fileReference.getInputStream();

OR

this.getClass().getResourceAsStream(filename);

Further reading Different ways of loading a file as an InputStream

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76