Java: NullPointerException from class.getResource( … )

I was writing a small application and when I tried to create an ImageIcon I always got an exception. The exception was caused by this line of code:

prayerLevel.setIcon(new ImageIcon(getClass().getResource("/icons/icon_prayer.png")));

Now within my program, the folder /icons/ does exist. I don't know if it makes the difference but the class file is within a package, where as the icons folder is within the project folder (when you would see the bin and src folder).

I have looked around for a bit and I couldn't find a solution that could help me solve the problem. Perhaps any of you guys could help?

Edit: someone asked for my folder hierarchy:

Folder Hierarchy

I know the class file is not in the same folder as the icons are, but I've made applications where I had to load files from a different folder and doing /folder/ always used to work.

Edit 2:

System.out.println(getClass().getResource("/icons/icon_prayer.png") == null);

Prints true.

6
задан Martin Tuskevicius 16 May 2011 в 20:47
поделиться