We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,489 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Null Exception at createImageIcon method

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
	}
3
Contributors
8
Replies
1 Year
Discussion Span
1 Year Ago
Last Updated
9
Views
mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
Skill Endorsements: 0

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

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

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

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
Skill Endorsements: 0
mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

hmmm aaaaaaaand what packages name contains yor Main Class

something like as ...

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

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

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

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
Skill Endorsements: 0
mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

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

mangopearapples
Posting Whiz in Training
242 posts since Oct 2010
Reputation Points: 26
Solved Threads: 3
Skill Endorsements: 0

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!

Moraes V.A.
Newbie Poster
1 post since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1066 seconds using 2.82MB