I'm currently learning about inserting an Image inside a JLabel. I'm following the documentation and instruction so far, but is still throwing a NullPointerException
. Am I missing a syntax or something?
I'm gonna send a pic to better understand the location and names of the package and files. Sorry for sending a pic.
Exception:
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.(ImageIcon.java:232) at com.mycompany.hotel_management_system.Log_In.(Log_In.java:28) at com.mycompany.hotel_management_system.Log_In.lambda$main$0(Log_In.java:329) at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714) 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: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)
Most likely the image you are trying to load is not found at the expected location.
getClass().getResource(...)
should return some URL but I suspect that is null. Divide the line into several, and check the result inbetween (via logging the URL, or just by comparing with null).
If that URL is null as I suspect, the resource you want to load does not exist. Add it to your classpath or correct the resource name.