简体   繁体   中英

Exception in thread “AWT-EventQueue-0” java.lang.IllegalArgumentException:

I am trying to run a program found here http://www.java2s.com/Code/Java/Advanced-Graphics/UnsharpMaskDemo.htm I simply created an Eclipse project for it an pasted it into a new class. The program code assumes that you have a picture file located somewhere

private void loadImage() {
    try {
        this.image = GraphicsUtilities.loadCompatibleImage(getClass().
                getResource("A.jpg"));
        this.image = GraphicsUtilities.createThumbnail(this.image, 300);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

I edit this to a file placed in the src folder. Strangely, the Exception above is not thrown and caught, regardsless of what path I provide. Instead i get an exception from a method somewhere later in the code:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at GraphicsUtilities.loadCompatibleImage(UnsharpMaskDemo.java:563)
    at UnsharpMaskDemo.loadImage(UnsharpMaskDemo.java:216)
    at UnsharpMaskDemo.<init>(UnsharpMaskDemo.java:99)
    at UnsharpMaskDemo$5.run(UnsharpMaskDemo.java:229)

No matter where I place the image or write the path, I get this. What am I doing wrong? I assume the program is well tested and would work out of the box...

getClass().getResource("A.jpg") tells the JVM to look for the file A.jpg somwhere on the classpath of the current class's classloader.

This explains why it works when putting it in the bin folder since this is on eclipse's classpath by default. You might also put it in your source-folder since then eclipse should copy it to the bin -folder while building.

To use another folder you can rightclick your eclipse-project, select Build Path -> Configure Build Path and on the Libraries -tab use Add Class Folder or Add External Class Folder to add the folder containing the image to the project's classpath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM