1

I want to load an image which is in my projet folder as : /src/images/URL.jpg

I tried this code :

BufferedImage image = ImageIO.read(getClass().getResource("/images/URL.jpg"));

But I'm getting this error :

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(ImageIO.java:1388)
    at Personel.PersonnelMainForm.print(PersonnelMainForm.java:464)

How can I solve this problem ?

Aimad Majdou
  • 563
  • 4
  • 13
  • 22
  • 1
    Show more code please. Is your image where you believe it is in your running classpath? Try and see if your `.getResource()` returns null, this is what I believe happens here. – fge Jun 16 '13 at 10:21

3 Answers3

4

From personal experience I use:

BufferedImage image = ImageIO.read(getClass().getResourceAsStream("/images/image.jpg"));

I get the resource as a stream and that seems to work fine for me.

Andrew Cumming
  • 965
  • 6
  • 14
2

You can try this version of read, which takes File as an argument.

BufferedImage image = ImageIO.read(new File("path"));

where path is the path to you file, absolute or relative as you need.

Another option, if you really want to load it as a resource, would be editing your classpath, as per this question.

Community
  • 1
  • 1
Humungus
  • 575
  • 2
  • 5
  • 16
0

I suppose you have a java class in the package. You have to move up so many times as package levels. Example: Java class is defined as org.test.MyClass you have to go up twice (../../) to be in the main directory.

albgorski
  • 107
  • 3