1

I've been at this for 20 minutes now and I just can't wrap my head around where I'm going wrong. If an image is in the same src folder as a java file, why doesn't this work:

Image image = ImageIO.read(getClass().getResource("/image.png"));

I'm not sure if I'm missing a trick or something but I've only been able to load image from http URLs which isn't very helpful for my project.

I know this is a silly question but I've looked at many other answers with similar titles but very different problems. There was also another answer that didn't work at all.

I feel like an idiot posting this but I'm honestly about to upload all my resources to a cloud service instead of having to deal with this.

user3530525
  • 691
  • 3
  • 8
  • 20
  • where is the image located ? – jmj Dec 28 '14 at 20:50
  • @JigarJoshi It's literally in the same package as the class that's loading it. – user3530525 Dec 28 '14 at 20:51
  • 3
    then remove front `/` change it to `ImageIO.read(getClass().getResource("image.png"));` – jmj Dec 28 '14 at 20:52
  • @JigarJoshi Apparently that loads a null image. – user3530525 Dec 28 '14 at 20:56
  • is the image present in classpath ? how do you run the program, does it have this image file placed next to the class file – jmj Dec 28 '14 at 21:02
  • @JigarJoshi https://rapidgrab.net/i/8bb9166c17ff.png that's how it's structured. Also, I've accounted for that fact that the file names are different than what I posted as the question. I just didn't want people suggesting ImageIcons. – user3530525 Dec 28 '14 at 21:03
  • eclipse compiles source to target directory (bin/classes usually) do you have classes and images there ? – jmj Dec 28 '14 at 21:05
  • @JigarJoshi Yeah, it's in both since everything else works except this one thing. – user3530525 Dec 28 '14 at 21:06
  • try with `/application/icon.png` – jmj Dec 28 '14 at 21:07
  • @JigarJoshi - That doesn't do it either. If it helps, I'm using this image in a TrayIcon so it throws either this exception: java.lang.IllegalArgumentException: creating TrayIcon with null Image or input == null depending on whether the '/' is there or not. – user3530525 Dec 28 '14 at 21:10
  • can you check your classpath at runtime by `System.out.println(System.getProperty("java.class.path"))` and go to each of those path and see if you have this image placed at `PATH_ENTRY/application/icon.png` ? – jmj Dec 28 '14 at 21:11
  • @JigarJoshi It returns: C:\Users\Colin\Desktop\Zuro Java\Pastr.me\bin;C:\Users\Colin\Desktop\eclipse\plugins\org.eclipse.fx.ide.css.jfx8_1.0.0.201408150702.jar – user3530525 Dec 28 '14 at 21:13
  • perfect now do you have `C:\Users\Colin\Desktop\Zuro Java\Pastr.me\bin\application\icon.png` file there ? – jmj Dec 28 '14 at 21:14
  • @JigarJoshi Yeah, it's in there. – user3530525 Dec 28 '14 at 21:15
  • ok If that is the case then I think space in path could be culprit, can you try renaming directory `Zuro Java` to `ZuroJava` if it is safe to do so – jmj Dec 28 '14 at 21:15
  • @JigarJoshi - I changed the folder name to Zuro_Java and reloaded eclipse. I've tried out all the different "icon.png" locations and it's still saying the input == null – user3530525 Dec 28 '14 at 21:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67811/discussion-between-jigar-joshi-and-user3530525). – jmj Dec 28 '14 at 21:19

2 Answers2

1

after this interesting discussion , we found that the image file was corrupted and that is why it was not reading (parsing) it to BufferedImage properly

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • Yeah it works now. I guess I shouldn't use online ico to png converters xD – user3530525 Dec 28 '14 at 21:44
  • you should probably report this issue to them – jmj Dec 28 '14 at 21:45
  • I will, turns out even photoshop can't parse the 'converted' file. https://rapidgrab.net/i/d54bb0c657a8.png I'm gonna assume the 'converter' just spoofs the file extension rather than actually converting the file format. – user3530525 Dec 28 '14 at 21:47
0

it should be in the same folder as your .class file not .java file! and furthermore you as others commented you should remove / from image address

hasan
  • 966
  • 2
  • 13
  • 40
  • It's in both because it runs but throws and exception so it'd still compiled w/ the java files. It just doesn't work regardless of the presence of the forward slash in the path. – user3530525 Dec 28 '14 at 21:22
  • what is the exception? and are you sure the code is not in an inner classe or anonymous inner class? – hasan Dec 28 '14 at 21:25
  • Well, the exception is that the input == null. – user3530525 Dec 28 '14 at 21:29
  • can you load the image with it's absolute path with something like? `ImageIO.read(new File("/path/to/your/file"))`? if no please put the stacktrace of the exception in your question definition. – hasan Dec 28 '14 at 21:32