1

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

Walter ZAMBOTTI
  • 301
  • 1
  • 2
  • 10
  • Possibly helpful answer, use "Clean & Build" rather than just "Run" or "Build": [netbeans & java, resourceMap.getString returning null](https://stackoverflow.com/questions/2043756/netbeans-java-resourcemap-getstring-returning-null) – sorifiend Feb 24 '21 at 03:09
  • Clean & Build already tried. Makes no difference! – Walter ZAMBOTTI Feb 24 '21 at 03:18
  • It's very odd that you get the error. Can you please edit your question to show a screenshot of your project explorer window, the one that shows the project structure and the packages like this: https://netbeans.org/images_www/articles/72/java/gui-image-display/project-with-imagepack.png – sorifiend Feb 24 '21 at 03:29
  • Sorry that took so long. I couldn't find any image hosting site that stackoverflow supported. Anyway i left a link! – Walter ZAMBOTTI Feb 24 '21 at 04:51

1 Answers1

1

(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".

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • the picture shows it in `src/main/`**`java`**!! ..my answer (and that's it) says: `src/main/`**`resources`** (eventually you have to create it manually & refresh project!) – xerx593 Feb 24 '21 at 05:26
  • You are right the my directory structure was slightly incorrect! I duplicated the other provided resource structure as you said and now the program runs! Why do I have to do that??? Can I get rid of the one that isn't working? – Walter ZAMBOTTI Feb 24 '21 at 05:29
  • it's ma(gic)ven ;) – xerx593 Feb 24 '21 at 05:30
  • yes it only appears in the target class when the gif is placed in the directory you mentioned. Why? And how should anyone know that? Is it an option I failed to set? Or should I have created the resources folder in a different location to begin with? – Walter ZAMBOTTI Feb 24 '21 at 05:34
  • best entry point for these questions: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html – xerx593 Feb 24 '21 at 05:36
  • ..then you'd have to dig (little) into "maven lifecycle" ,... "maven built-in plugins" (sources, resources). And I just slipped over that (detail), having *much* experience with "old tutorials & newest netbeans version/maven" :) – xerx593 Feb 24 '21 at 05:40
  • So after creating that directory manually I can see back in the Project that it has been placed under 'Other Sources' and not 'Source Packages'. – Walter ZAMBOTTI Feb 24 '21 at 05:40
  • Aiight! ..that's the good about Netbeans (>=7/8??): the neat maven integration (in eclipse you'd have to "refresh maven project"..) – xerx593 Feb 24 '21 at 05:42
  • and having "binaries" in the source folders is rather "old school" (ant like), whereas maven/gradle/modern build tools distinguish between "sources" and "resources" (which is also cool!) Glad to have helped, thx for the rep. & happy coding! ;) – xerx593 Feb 24 '21 at 05:49