0

I'm trying to set an icon for a label like this:

jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/mycompany/footballist/pics/search3030.png")));

It's throwing the error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.desktop/javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at com.mycompany.footballist.SearchFrame.<init>(SearchFrame.java:18)
at com.mycompany.footballist.StartingFrame.jButton1ActionPerformed(StartingFrame.java:129)
at com.mycompany.footballist.StartingFrame$2.actionPerformed(StartingFrame.java:65)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6635)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6400)
at java.desktop/java.awt.Container.processEvent(Container.java:2263)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5011)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4843)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4547)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4843)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

My project structure is like that:

enter image description here

I'm trying to access this photo from SearchFrame.java and they are under the same package. Image is in the pics folder. I don't know what am I doing wrong. Can you please help me?

I moved the file to the same package with SearchFrame.java, but nothing changed. Still the same error.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Does this answer your question? [Java getClass().getResource("file") leads to NullPointerException](https://stackoverflow.com/questions/5769351/java-getclass-getresourcefile-leads-to-nullpointerexception) – Vasim Hayat Dec 30 '22 at 14:50
  • https://stackoverflow.com/questions/5769351/java-getclass-getresourcefile-leads-to-nullpointerexception this question will help you... – Vasim Hayat Dec 30 '22 at 14:50
  • No, it didn't work. Still the same error. Even I moved the image to the same package with .java file. –  Dec 30 '22 at 14:58
  • What are you using for building? Are you sure it is configured to copy resources like `png` to the output directory? – Mark Rotteveel Dec 30 '22 at 15:03
  • What do you mean by building? If I'm understanding this correct, I'm using Maven with NetBeans. And I just copied the image from my desktop to the ``com.mycompany.footballist`` folder. –  Dec 30 '22 at 15:07
  • 1
    *And I just copied the image from my desktop to the com.mycompany.footballist folder*. But that's wrong - it should be com.mycompany.footballist.pics `jar tf whatever-distribution-jar.jar | find "search3030"` (assuming Windows). Personally I wouldn't use such a deep resources folder – g00se Dec 30 '22 at 15:13
  • I'm using Windows. Developing the project with NetBeans, Maven and Swing. Sorry(I'm a newbie on Java) I didn't understand your answer. ``jar tf whatever-distribution-jar.jar | find "search3030"`` what is this? What should I do with this? –  Dec 30 '22 at 15:21
  • 1
    In cmd.exe change to the directory in which your runnable jar is created and run that command. Tell us what you get – g00se Dec 30 '22 at 15:26
  • Ok. what should I type instead of ``whatever-distribution``? –  Dec 30 '22 at 15:44
  • 1
    You'll have to find out the name of the output jar that gets created – g00se Dec 30 '22 at 15:47
  • I think I found it. And I've ran this command: ``jar tf Footballist-1.0-SNAPSHOT.jar | find "search3030"`` nothing happened. –  Dec 30 '22 at 15:49
  • 1
    Yes, that looks like the right kind of file name. If nothing is found, it means that nothing has got in there. Do the following: Create *directory* /src/main/resources/images in your Maven source tree (most will already be there). Copy the png there. Use the following code: `jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/search3030.png")));` Do that right and you should be able to run `java -jar Footballist-1.0-SNAPSHOT.jar` and have it work and find the image – g00se Dec 30 '22 at 16:02
  • 1
    Good. I'll post it as an answer for you to accept – g00se Dec 30 '22 at 16:11

1 Answers1

0
  • Create directory /src/main/resources/images in your Maven source tree (most will already be there).

  • Copy the png there.

  • Use the following code:

jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/search3030.png")));

Do that right and you should be able to run java -jar Footballist-1.0-SNAPSHOT.jar and have it work and find the image

g00se
  • 3,207
  • 2
  • 5
  • 9