-3

i am using maven in netbeans and i have a frame where i want to add a logo to. in the design part of the jframe i can see the logo but when i run the app it gives me an error saying that the location is null. however my code works without the icon.

ps i have added the icon through the gui builder of netbeans in a label!

enter image description here

enter image description here

and here is the error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null
    at java.desktop/javax.swing.ImageIcon.<init>(ImageIcon.java:217)
    at com.mycompany.moduleborne.BorneMain.initComponents(BorneMain.java:416)
    at com.mycompany.moduleborne.BorneMain.<init>(BorneMain.java:77)
    at com.mycompany.moduleborne.Initt.jButton1ActionPerformed(Initt.java:117)
    at com.mycompany.moduleborne.Initt.access$000(Initt.java:18)
    at com.mycompany.moduleborne.Initt$1.actionPerformed(Initt.java:50)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
    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:6617)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6382)
    at java.desktop/java.awt.Container.processEvent(Container.java:2264)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4993)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2322)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4934)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4563)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4504)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2308)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2773)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
    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(AccessController.java:391)
    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(AccessController.java:391)
    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)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

3

Under maven only java sources come under src/main/java/. On the other hand resources come under src/main/resources/.

You can simple create the folder(s) under Project Files, nothing special.

File names must be case-sensitive. Only Windows is case-insensitive. The compiled product packed in a jar (zip format) is case sensitive. As is Linux.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • i didn't understand clearly your answer dear joop can u elaborate more on the steps needed ps: i need it to work on different laptops not just this one i can make it with an absolute path but i want it to be inside my app wherever i run it the logo appears inside it – regragui karim Oct 15 '21 at 13:21
  • 1
    As resource, inside the app (with the classes on the class path) is most often the best solution. Maven per default has the convention that those resources are in another path than the java sources. Class files and resource files then are collected together again in the jar (a zip format). In your case you must copy the packages/subfolders under src/main/resources. – Joop Eggen Oct 15 '21 at 13:35
  • and what should i wirte as path here ? jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ressources/logo13percent.png"))); – regragui karim Oct 15 '21 at 13:38
  • 1
    No, like "src/main/java" as source root, "src/main/resources" is the resources root. Resources with one 's' (and one at the end). "Ressource" is the German spelling ;). So `"/logo13percent.png"` if the file is at the root. – Joop Eggen Oct 15 '21 at 14:26
  • i'm sorry for bothering u with my question and thank you for ur patience! so, if i use "/logo13percent.png" as path in the code then, i should insert it in "src/main" if not, can you tell me what should i write as path in the code if i put it in the resources folder i hope this will be my last comment and thank you a lot for your help and comment appreciate it – regragui karim Oct 15 '21 at 15:11
  • 1
    `src/main/resources/logo13percent.png` and `getResource("/logo13percent.png")` – Joop Eggen Oct 15 '21 at 18:13
  • thank you a lot again for ur help ! i can't thank you enough for helping me underrstand and solve this issue! finally i can use images inside my apps. cheeeers! – regragui karim Oct 22 '21 at 15:53