2

I am currently trying to simply display an image within a JFrame. I have tried using the below code but it doesn't seem to work. I can't work out where I am going wrong - any ideas?

This is the error message displayed in the console:

Exception in thread "main" java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(Unknown Source)
    at Day1.PictureTester.<init>(PictureTester.java:22)
    at Day1.PictureTester.main(PictureTester.java:14)

Here is the code:

import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class PictureTester extends JFrame{

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        new PictureTester().setVisible(true);
    }

    public PictureTester(){
        super();
        setSize(600, 600);
        setLayout(new GridLayout());
        java.net.URL imgUrl = PictureTester.class.getResource("C:\\Users\\Harry\\Desktop\\LearnJava\\pug-image3.jpg");
        ImageIcon image = new ImageIcon(imgUrl);    
        JLabel display = new JLabel(image);

        add (display);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Pineapple
  • 91
  • 7
  • Hopefully, the thread regarding, how to [add images to Java Project](http://stackoverflow.com/a/9866659/1057230), will be of some help on the topic :-) – nIcE cOw Jul 12 '15 at 16:45

4 Answers4

4

Of course that won't work. Read the javadoc of Class.getResource(). It uses the ClassLoader to load a resource from the classpath, using a slash separated path.

Put your image in the same package as the PictureTester class, and just use

PictureTester.class.getResource("pug-image3.jpg");

Or put it in some random package of your source folder (like com.foo.bar), and load it using

PictureTester.class.getResource("/com/foo/bar/pug-image3.jpg");
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Hello JB Nizet, thank you for your input. I have adjusted the code accordingly, but I am still faced with the same error message as stated above. Any ideas? – Pineapple Jul 12 '15 at 17:23
  • So, what's the new code, and where did youput the file? How do you build the project? Which IDE are you using? Is it a gradle or Maven project? – JB Nizet Jul 12 '15 at 17:52
  • IDE: Eclipse Gradle/Maven: Don't know. Filed placed: in a new source folder within the package. New code: `package Day1; import java.awt.GridLayout; import javax.swing.JFrame; public class PictureTester extends JFrame{ private static final long serialVersionUID = 1L; public static void main(String[] args) { new PictureTester().setVisible(true); } public PictureTester(){ setSize(600, 600); setLayout(new GridLayout()); PictureTester.class.getResource("pug-image3.jpg"); } }` – Pineapple Jul 12 '15 at 18:47
1
You can do it like the following example, by using just an image and a label. also you can remove all the extra code if you intend to use this image only once.

import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class PictureTester extends JFrame{

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        new PictureTester().setVisible(true);
    }

    public PictureTester(){
        super();
        setSize(600, 600);
        setLayout(new GridLayout());
        add(new JLabel(new ImageIcon("Path/To/Your/Image.png")));
    }
}
xhulio
  • 1,093
  • 1
  • 13
  • 32
1

This code solved it, it helped my thinking to create it within a new project:

package NewAppIdea;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


    public class PictureSplash extends JFrame {

        private static final long serialVersionUID = 1L;
        JLabel l1;

        PictureSplash(){
        setTitle("Pic");
        setSize(400,400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setLayout(new BorderLayout());
        setContentPane(new JLabel(new ImageIcon("C:\\Users\\Harry\\Desktop\\LearnJava\\pug-image3.jpg")));
        setLayout(new FlowLayout());
        setSize(399,399);
        setSize(400,400);
        }

        public static void main(String args[]) {
            new PictureSplash();
        }   
    }
Pineapple
  • 91
  • 7
0
        Icon i=new ImageIcon("src//pics//scope.jpg");
        JLabel l1=new JLabel(i);

Use simply like this use JLabel or ImageIcon

A.K.
  • 2,284
  • 3
  • 26
  • 35