1

My program runs fine in Netbeans, but I get the following error when I run my applet in a browser:

java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)

I opened the jar to verify that the png files are correctly placed there. I'm not certain why the following doesn't work (in a try block, of course):

BufferedImage beam = ImageIO.read(this.getClass().getResource("images/beam.png"));

I've tried other things like the following, but suspect the problem might be something else.

URL url = this.getClass().getResource("images/beam.png");
BufferedImage beam = ImageIO.read(url.openStream());

Your advice is appreciated.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Paul Lombardi
  • 131
  • 2
  • 2
  • 9
  • `URL url = this.getClass().getResource("images/beam.png");` The image is in a sub-directory of the package of the calling class? To make that path from the root of the class-path, make it `URL url = this.getClass().getResource("/images/beam.png");` And be sure to print out the URL or inspect it in a debugger. It seems like you are running on guesses at the moment. – Andrew Thompson Oct 11 '12 at 14:25
  • 1
    Can you explain how you've structured your JAR file where the class and images are placed? Might help trigger some ideas. – jheddings Oct 11 '12 at 14:32
  • myJar.jar is in the same directory as lib. The jar has directories mypackage and META-INF. The mypackage directory has the class files and the directory images. The images directory has the png files. I think the path "images/beam.png" is relative to the class files. – Paul Lombardi Oct 11 '12 at 15:02
  • Sorry that my responses are delayed. I'm working on this with people coming in and out of my office. – Paul Lombardi Oct 11 '12 at 15:40
  • 1
    Add @PersonName if you want someone notified of a comment. – Andrew Thompson Oct 12 '12 at 05:29
  • *"I think the path "images/beam.png" is relative to the class files."* Our thinking aligns, but to make that path explicit use `/mypackage/images/beam.png` as the path string. – Andrew Thompson Oct 12 '12 at 05:31

2 Answers2

1

The problem is in your path String. Use this instead:

BufferedImage beam = ImageIO.read(this.getClass().getResource("/images/beam.png"));

(Note the / before the path)

Gilberto Torrezan
  • 5,113
  • 4
  • 31
  • 50
0

For the record and If someone is stucked with this type of error for some reason eclipse auto changed the build path and blocked the image path from being packaged.

To change this setting just right click on the project name -> Properties-> Java Build Path-> Source Tab and check that the folder containing the images is not excluded, if it is just remove that rule.

Karlo A. López
  • 2,548
  • 3
  • 29
  • 56