Hey everyone,
I'm trying to make a button have an image, it used to work but for some reason, now it doesn't.
This is the error:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at Main.createImageIcon(Main.java:109)
at Main.<init>(Main.java:82)
at Main.main(Main.java:103)


I see that the error is the:

ImageIcon NewFileIcon = createImageIcon("Images/NewFileIcon.png");
ImageIcon LoadFileIcon = createImageIcon("Images/LoadFileIcon.png");
ImageIcon SaveFileIcon = createImageIcon("Images/SaveFileIcon.png");
ImageIcon OptionsIcon = createImageIcon("Images/OptionsIcon.png");

I see nothing wrong with it...
It's not in the main method or my Main method.
public static void main(){}
public Main(){}

It used to work which is what makes me think I'm stupid, it must be obvious but well... Not to me!

Important bits:

public class Main implements KeyListener{
	ImageIcon NewFileIcon = createImageIcon("Images/NewFileIcon.png");
	ImageIcon LoadFileIcon = createImageIcon("Images/LoadFileIcon.png");
	ImageIcon SaveFileIcon = createImageIcon("Images/SaveFileIcon.png");
	ImageIcon OptionsIcon = createImageIcon("Images/OptionsIcon.png");
	public static void main(String[] args) {
		if(args.length > 0){
			fileLoadPath = args[0];
		}
		new Main();
		
	}
	public ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = Main.class.getResource(path);
		
	    return new ImageIcon(imgURL);
	}
	public Main(){
		JButton New = new JButton(NewFileIcon);
		//Other buttons too
	}

I hoping that you created folder with name Images

there are lots of ways:

button.setIcon(new ImageIcon(this.getClass().getClassLoader().getResource("Images/iconName")));

but I still prefered

Image ViewFxImage = ImageViewFxLoader.getImage(ViewFxDeals.class, "resources/passed.png");
final Icon ViewFxIcon = new ImageIcon(ViewFxImage);
myButton.setIcon(null); //remove Icon
myButton.setIcon(ViewFxIcon); //add Icon

class ImageViewFxLoader {

    private ImageViewFxLoader() {
    }

    public static Image getImage(Class<?> relativeClass, String filename) {
        Image returnValue = null;
        InputStream is = relativeClass.getResourceAsStream(filename);
        if (is != null) {
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                int ch;
                while ((ch = bis.read()) != -1) {
                    baos.write(ch);
                }
                returnValue = Toolkit.getDefaultToolkit().createImage(baos.toByteArray());
            } catch (IOException exception) {                
                SaveEventToDb.errorWritter();
            } finally {
                try {
                    bis.close();
                    baos.close();
                    bis = null;
                    baos = null;
                } catch (IOException ex) {
                    Logger.getLogger(ImageViewMmLoader.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return returnValue;
    }
}

resolutions in 16x16 or 32x32 in pixels

I did create a folder named Images. It even worked when it wasn't created but for some reason it doesn't work now.

hmmm aaaaaaaand what packages name contains yor Main Class

something like as ...

myPackage
- Main
myPackage/Images
- xxx.png
--yyy.png

I export it first, then run it with the image folder in the same folder as the JAR file...

I'm using Eclipse.
I don't think it's anything to do with the actual file, I think it's the code but I'm probably wrong :S

Sorry, My English is not so good and I found this issue,on mar 2012, when I had the same problem. Perhaps someone is going to search this page yet!
Solution:

On the eclipse IDE, using Java Project, you should 1) create in then src folder a new package (right mouse click...new..package), like this: com.yourproject.image 2) then, import (right mouse click..import..) of your images in this package 3) in your code, you should use something like this... new ImageIcon(getClass().getResource( "/com/yourproject/image/iconImage.gif")); - Note that we are using the package like a path here! and it´s all!

A Good professional work, for everybody!

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.