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:
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…