I get the following exception

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null

My code is

     java.net.URL auth = getClass().getResource("auth.jpg");
    Icon auth1 = new ImageIcon(auth);
    java.net.URL alert = getClass().getResource("alert.jpg");
    Icon alert1 = new ImageIcon(alert);
    java.net.URL insuf = getClass().getResource("insuf.jpg");
    Icon insuf1 = new ImageIcon(insuf);
    java.net.URL inva = getClass().getResource("inva.jpg");
    Icon inva1 = new ImageIcon(inva);
    URL see = getClass().getResource("see.jpg");
    Icon see1 = new ImageIcon(see);
    java.net.URL tap = getClass().getResource("tap.jpg");
    Icon tap1 = new ImageIcon(tap);
    java.net.URL wel = getClass().getResource("wel.jpg");
    Icon wel1 = new ImageIcon(wel);

Screenshot_2021-07-26_144320.png

Recommended Answers

All 10 Replies

It looks like your files are in the wrong place.
As used in your code, getResource will look for the files in same folder (or package if you are runnng from a jar) that the current class was loaded from. I don't see any class files in that directory.

It looks like you are using NetBeans?
If so, create a package called (eg) images in your project's Source Packages alongside the package containing your source code. Add all the image files to it. Reference them like getClass().getResource("/images/auth.jpg");
Netbeans will create the correct folder or jar struture for you and put things in the right place.

Here's an example

Screenshot_2021-07-26_at_12_51_44.png

Screenshot_2021-07-26_170336.png

I still get the same issue

The thing is that i want to set images to my labels. I have located the images using the pallette but when i execute the jar in another machine it does not show the images.I wanted to fix it and saw that this is the approach but it does not work

Yes, this is the right way, and will work just fine when NetBeans puts it all in a Jar.
We need to see the full Stack trace from the exception, and the exact line of your code that it refers to.

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.smarttraveller.Form.<init>(Form.java:73)
at com.mycompany.smarttraveller.Form$8.run(Form.java:625)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
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.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
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)

And what exactly is at com.mycompany.smarttraveller.Form.<init>(Form.java:73) (and the preceeding few lines)

ps (may be nothing)

at com.mycompany.smarttraveller.Form.<init>(Form.java:73)
at com.mycompany.smarttraveller.Form$8.run(Form.java:625)

that seems a bit odd. Form$8 is an anonymous inner class of Form, but it seems to be ceating a new instance of Form.
Is that what you intended?

Now it shows at 61

Screenshot_2021-07-26_181042.png

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.smarttraveller.Form.initComponents(Form.java:405)
at com.mycompany.smarttraveller.Form.<init>(Form.java:61)
at com.mycompany.smarttraveller.Form$8.run(Form.java:625)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
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.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
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)

Its the other one, line 405, where the problem starts. What's on that?

error.JPG

This post has no text-based content.

Manu_18, can you show your code? Better yet, start a new topic and show your code and the error message. This way your question doesn't get lost inside a 2 year old topic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.