0

Can anyone help why am I getting error for my Java swing application that makes use of

ImageIcon(getClass().getResource()

to load images as shown below. Thanks in advance.

Code where error is shown:

jButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/folder63.png"))); 

Error description:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at frame.foundation.initComponents(foundation.java:282)
at frame.foundation.<init>(foundation.java:21)
at frame.foundation$127.run(foundation.java:3453)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

PS: I have also added "images" folder as the source folder in my project

Folder Structure:

myprojectName  
         |  
         |___src  
         |     |  
         |     |___frame //(is my package)  
         |            |  
         |            |__foundation.java // main class  
         |  
         |__images  
         |    |  
         |    |__folder63.png  
         |    |__d.jpg  
         |    |__e.jpg  
         |    |__f.jpg    
Braj
  • 46,415
  • 5
  • 60
  • 76
victoryVSK
  • 41
  • 1
  • 3
  • 12
  • 3
    To those who approved this edit proposed by @Darshan: Seriously, you need to read what you're reviewing. The editor deleted part of the question, which now no longer makes sense. – David Makogon Jul 26 '14 at 11:44
  • http://stackoverflow.com/questions/5769351/java-getclass-getresourcefile-leads-to-nullpointerexception?rq=1 checked with this but still the error pops up – victoryVSK Jul 26 '14 at 11:47
  • 1
    @DavidMakogon +1 I put 2 parts back in the question after seeing the attempted edit was already... aceppted ?! – FelipeAls Jul 26 '14 at 11:49
  • Be sure to check the case in the path name. `"/images/folder63.png"` != `"/images/Folder63.png"` – Andrew Thompson Jul 26 '14 at 11:53
  • @AndrewThompson: double checked such things. Its all correct. Still getting error. – victoryVSK Jul 26 '14 at 11:55
  • are you using it in Applet? – Braj Jul 26 '14 at 12:09
  • @braj no just a java swing application. Iam still studying on that link you provided. But error is still with it. – victoryVSK Jul 26 '14 at 12:21
  • have you tried all the options. move the image in some other folder. – Braj Jul 26 '14 at 12:23
  • @FelipeAls - nicely done, including both the code and the PS (which actually contained question-specific info). – David Makogon Jul 26 '14 at 12:38
  • @braj : tried in different foldername and is showing the same exception – victoryVSK Jul 26 '14 at 12:53
  • please share snapshot of the project structure. – Braj Jul 26 '14 at 12:53
  • @braj : i couldnt upload the pic, so i made my folder structure in the question itself. – victoryVSK Jul 26 '14 at 13:15
  • the problem is clear. read what I have told you. It's parallel to `src` folder and as I said you it should be under `src` folder. or use `ImageIO.read(new File("images/folder63.jpg"));` as 2nd option mentioned in my post. – Braj Jul 26 '14 at 13:18
  • you are not reading my post carefully and you have wasted your 1 hour to figure this issue.... – Braj Jul 26 '14 at 13:19
  • @Braj: its clear that "images" folder is parrallel with src folder , so i have to use:" jButton9.setIcon(new javax.swing.ImageIcon(getClass().getResource("images/folder63.png"))); " – victoryVSK Jul 26 '14 at 13:23
  • @Braj : tried with the above code , but that exception is still coming. – victoryVSK Jul 26 '14 at 13:24
  • now I am afraid. As you have tried other options as well moving image in other folder as well. – Braj Jul 26 '14 at 13:25
  • executed finally from that exception.... I think when the execution envnmt is changed from " CDC- 1.1/ Foundation" to "J2SE 1.5(JRE8)", made the excecution. – victoryVSK Jul 26 '14 at 13:54
  • possible duplicate of [Java in Eclipse: Where do I put files on the filesystem that I want to load using getResource? (e.g. images for an ImageIcon)](http://stackoverflow.com/questions/270197/java-in-eclipse-where-do-i-put-files-on-the-filesystem-that-i-want-to-load-usin) – Barett Jun 24 '15 at 17:02

2 Answers2

5

Make sure image file is present at correct location. It should be under src/images folder.

You can try any one based on image location.

// Read from same package 
ImageIO.read(getClass().getResourceAsStream("folder63.png"));

// Read from images folder parallel to src in your project
ImageIO.read(new File("images/folder63.jpg"));

// Read from src/images folder
ImageIO.read(getClass().getResource("/images/folder63.png"))

// Read from src/images folder
ImageIO.read(getClass().getResourceAsStream("/images/folder63.png"))

Read more...

It's worth reading Java Tutorial on Loading Images Using getResource

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
-1

Try this

InputStream input = classLoader.getResourceAsStream("image.jpg");
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Asad Mukhtar
  • 391
  • 6
  • 18
  • This line of code doesn't make a ton of sense on its own. Try including some more details on how to interface this recommendation with `ImageIcon`, and why adding an InputStream would be helpful over the other ImageIcon constructors. – Barett Jun 20 '15 at 19:58