0

Icon - image within project Got errors

Icon - external image (file path) No errors

I'm trying to set the icon image using the image within project method, but it keep got exception error. How to solve the exception error shown in Figure 1 ? Because when i opened the project in another pc, all the icons were dissappeared.

Error code generated

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:234)
at com.mycompany.javaassignment.NewJFrame.initComponents(NewJFrame.java:36)
at com.mycompany.javaassignment.NewJFrame.<init>(NewJFrame.java:19)
at com.mycompany.javaassignment.NewJFrame$1.run(NewJFrame.java:96)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:771)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:741)
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)
  • Dont post screenshots with the error message, please post the Stacktrace. Show the lines of code which cause the problem. What have you tried? – Moritz Schmidt May 08 '22 at 08:10
  • I'm trying to set the icon image using the image within project method, but it keep got exception error. However, when I using the external image method, it works tho. But the cons of using external image is when the project is open in another pc, all the icons disappeared. @MoritzSchmidt – Patrick Lee May 08 '22 at 08:17
  • 1
    The answer to this [Stack Overflow question](https://stackoverflow.com/questions/9864267/loading-resources-like-images-while-running-project-distributed-as-jar-archive) should help. – Gilbert Le Blanc May 08 '22 at 08:39

0 Answers0