0

I am Using Windows 10, Apache NetBeans, maven swing project;

maven project structure:

|-- projectname
    |-- Source Packages
    |   |--package1
    |   |--package2
    |   |--resources
    |   |   |--images
    |   |   |   |--image.png

Though NetBeans displays nested packages/folders like this resources.images

project structure

Inside NetBeans, When I right-click on image.png then click Properties then click (All Files ...) it gives me this path:

C:\Users\%USERNAME%\Documents\NetBeansProjects\projectname\src\main\java\resources\images\image.png

How to reference image path?

I tried many paths, also many answers from this site, but non solved my problem. The output always gives Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

java.net.URL u = getClass().getResource("/resources/images/image.png");
ImageIcon icon = new ImageIcon(u);
JLabel label1 = new JLabel("Image and Text", icon, JLabel.CENTER);
Saleh Rezq
  • 193
  • 1
  • 13
  • So, "resources" for maven go in the `{project folder}/src/main/resources` folder. From within your code, you don't need to reference `resources`, just sub folders (ie `/images/tick.png`) – MadProgrammer Apr 21 '22 at 22:50
  • @MadProgrammer I also tied this exact path with the preceding slash, but same error. – Saleh Rezq Apr 21 '22 at 22:58
  • But based on the directory structure you've provided, it's in the wrong directory for maven, in fact, your directory structure is wrong for maven generally – MadProgrammer Apr 21 '22 at 23:01
  • @MadProgrammer I uploaded a photo, if this is not proper location, then how to properly create a `resources` directory in maven. – Saleh Rezq Apr 22 '22 at 01:31
  • 1
    You might want to have a read through https://stackoverflow.com/questions/69308166/netbeans-12x-mavan-include-and-load-embedded-resources – MadProgrammer Apr 22 '22 at 01:37

0 Answers0