-1

I'm trying to implement a PNG in a JLabel, but when I run the program using the path that NetBeans give me automatically, it throws me this error:

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

But in the design it loads the images normally.

This is one piece of code where I'm trying to implement a PNG:

jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/src/icons8-education-64.png")))

I have the images and the main class in the same package.

camickr
  • 316,400
  • 19
  • 155
  • 279
  • 2
    I'm guessing the directory "src" isn't accessible at runtime (as opposed the the IDE). Some more details about exactly how you are running this code when you get the error would help. – markspace Jun 03 '22 at 00:50
  • 2
    Don’t reference src in your code, the path won’t exist once the app is exported – MadProgrammer Jun 03 '22 at 01:15
  • I only have one archive where I have all the code for the graphic interface and the main. And when I run (only clicking run file) that archive, it throws that error. – Diego Mellizo Jun 03 '22 at 01:19
  • Does this answer your question? [File loading by getClass().getResource()](https://stackoverflow.com/questions/14089146/file-loading-by-getclass-getresource) AS mentioned in the comment above, the `src` folder is not present once the application is compiled, so in short just remove `/src/` from your code and it should function correctly. – sorifiend Jun 03 '22 at 01:26
  • I tried removing the `src` but it didn't work. So I tried to write the code by myself instead of let NetBeans creating it, and with that change it worked. – Diego Mellizo Jun 03 '22 at 01:36

0 Answers0