0

i dont know why this simple program is not working

import javax.swing.*;
public class Main {    
public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(600,600);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ImageIcon img = new ImageIcon(frame.getClass().getResource("/images/4.png"));
    JLabel lbl1 = new JLabel(img);        
    frame.add(lbl1);       
    frame.setVisible(true);           

}    

when i run the program this error appears :

Exception in thread "main" java.lang.NullPointerException
at java.desktop/javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at graphic_example9.Main.main(Main.java:12)

as you see in this picture i added images directory to my project directory : enter image description here

(my os is debian 10.3 and my IDE is netbeans 11.3)

Alien_xxx
  • 17
  • 3
  • Most likely, the image file is not available in the resources in the mentioned location. [This answer](https://stackoverflow.com/questions/4301329/java-class-getresource-returns-null) may help you. – Nowhere Man Apr 30 '20 at 17:58
  • Try ../images/4.png – Nitin Bisht Apr 30 '20 at 18:00
  • these recommendation doesn't work for me – Alien_xxx Apr 30 '20 at 18:30
  • From the _javadoc_ of method [`getResource`](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResource-java.lang.String-) _If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'._ Where, exactly is file `4.png` located. What is its full path? And what is the full path to file `Main.class` ? – Abra Apr 30 '20 at 19:16
  • i used full path to the file before i post this topic and its not working – Alien_xxx Apr 30 '20 at 19:26
  • I did ___not___ ask if you tried the full path. I asked you to post the full path for file `4.png` and file `Main.class`. If you provide those details, I may be able to help you. – Abra May 01 '20 at 15:23

0 Answers0