0

The following code will only work on my computer but not on others:

doll.setIcon(new ImageIcon("C:/Users/akashsharma/Documents/NetBeansProjects/Hangman/src/win/images/G.jpg"));

I have tried using the following but it results in a NullPointerException.

getClass().getResource("/images/G.jpg")

What should I do to solve this problem?

The arrangement of files in the IDE

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Akash Sharma
  • 11
  • 1
  • 4
  • Do you notice the difference between `../G.jpg` & `../G.png`? Try `/win/images/G.jpg` & if that doesn't work, describe the directory structure in the IDE, down to the image. – Andrew Thompson Feb 12 '17 at 07:42
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Code-Apprentice Feb 12 '17 at 07:46
  • Please show the stack trace and the line of code which causes the crash. – Code-Apprentice Feb 12 '17 at 07:46
  • The path for getResource would need to be /win/images/G.jpg. Based on your current context, remember, the root search path starts from src – MadProgrammer Feb 12 '17 at 08:12
  • @AndrewThompson sorry i mistyped the question it will be G.jpg and not G.png. – Akash Sharma Feb 12 '17 at 08:19
  • @MadProgrammer I followed your suggestion and now i am not getting a nullPointerException but still the icon is not being set. – Akash Sharma Feb 12 '17 at 08:22
  • 1
    @AkashSharma Then the image isn't where you think it is. Verify that the is stored in `win/images/G.jpg` within your project, make sure it's visible from within netbeans project, make sure you perform a clean and build. As a last resort, unzip the Jar file – MadProgrammer Feb 12 '17 at 08:24
  • @MadProgrammer https://i.stack.imgur.com/4oPQL.jpg – Akash Sharma Feb 12 '17 at 08:32
  • @Akash Sharma: You are referencing to your file with an absolute path. In your case "C:/Users/akashsharma/Documents/NetBeansProjects/Hangman/src/win/images/G.jpg". Are you sure, that the parent folders "/Users/akashsharma/Documents/NetBeansProjects/" exist on the "other" computers you want to run your program on? I strongly recommend to use relative paths, so that you're not dependend on the file structure outside/above your program's. –  Feb 12 '17 at 09:16
  • @MadProgrammer *"As a last resort, unzip the Jar file"* Good idea. I suspect Netbeans expects resources to go in a `resources` directory and won't include them in the Jar unless they are in that (or a sub-directory of it). – Andrew Thompson Feb 12 '17 at 09:49
  • @AndrewThompson Unless it's a maven project, Netbeans won't (generally) care, but when you have problems, seeing what's in the Jar is always a good idea :P – MadProgrammer Feb 12 '17 at 11:12
  • @AkashSharma: Please have a look at [Loading Image Resource](http://stackoverflow.com/a/9866659/1057230). I hope it will be of some help on the topic :-) – nIcE cOw Feb 12 '17 at 17:56

1 Answers1

0

If it only works in your computer then it is a directory error.

Solution:

  • Import the image to your project
  • Use the following code

    jLabel.setIcon(new ImageIcon(getClass().getResource("/FOLDER_NAME/G.jpg")));
    
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ammar H Sufyan
  • 97
  • 1
  • 3
  • 13