Issue
So I followed the netbeans (I'm using 12.2) tutorial for handling images:
https://netbeans.org/kb/73/java/gui-image-display.html?print=yes
I can see the icon in the designer but when run getResource() returns a null indicating it can't find the resource.
I have found this question which seemed to be related:
Netbeans ImageIcon not displaying
I have ensured that I am using a package and not a folder.
The error generated :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.desktop/javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at com.byotrader.zom.ZomFrame.initComponents(ZomFrame.java:37)
The line of code:
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/byotrader/zom/resources/ZomIcon.gif"))); // NOI18N
I have no idea why this is failing when run.
So my introduction to netbeans isn't going as smoothly as I hoped!
As requested project screen shot:
https://photos.app.goo.gl/CJ4EB3PD4a86nAVK8
Any ideas what I have failed to do?
One suggested answer relating to the following question :
netbeans & java, resourceMap.getString returning null
However the answer clearly states this is only a problem if a Clean build has not been done. That is not my issue as I have tried a clean build!
However looking in the .jar file I can see the picture resources have not been added! I assume this is the problem. However I have no idea how I can force the IDE to do its job!
Link to files structure:
https://photos.app.goo.gl/bRxHHW2UtRjPF7LeA
This link may also help others in future:
Netbeans Maven Project - Other Sources does not appear
Solution
(It is a Maven-Project!*) Put your image under:
src/main/resources/com/byotrader/zom/resources/ZomIcon.gif
../resources/...
Explanation:
Netbeans (when on a maven project) completely (except maybe in designer mode!;) builds with maven! ...which means... no gif (etc.) files are considered form src/main/java
... (check your target/classes
folder!)
After my fix proposal, the image should be in the desired location.
* The tutorial(, which explicitly tells "Java Project") is little outdated for Netbeans 12.2, where there is no "Java Project"... possible alternatives are (maven/gradle) "Java Frontend Project" (requires javaFX!) or the "good ole" (ant) "Samples>Java With Ant".
Answered By - xerx593