Dion V.
Dion V.

Reputation: 2120

ImageIO Exception

I am getting the following error:

javax.imageio.IIOException: Can't read input file!
Resource not found: C:\icon.gif
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at connector.SystemTrayCreator.createImage(SystemTrayCreator.java:98)
at connector.SystemTrayCreator.create(SystemTrayCreator.java:36)
at connector.Start.main(Start.java:14)
Exception in thread "main" java.lang.IllegalArgumentException: creating TrayIcon with     null Image
at java.awt.TrayIcon.<init>(TrayIcon.java:168)
at connector.SystemTrayCreator.create(SystemTrayCreator.java:36)
at connector.Start.main(Start.java:14)
Java Result: 1

Out of the following code:

final TrayIcon trayIcon = 
            new TrayIcon(createImage("C:\\icon.gif", "Tray icon"));

with

protected static Image createImage(String path, String description) {
    Image image = null;

    try {
        File f = new File(path);
        image = ImageIO.read(f);
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (image == null) {
        Debugger.showMessage("Resource not found: " + path);
        return null;
    } else {
        return (new ImageIcon(image, description)).getImage();
    }
}

I am quite sure that the path is correct. Also, the names match.

Anyone knows what's wrong?

Upvotes: 2

Views: 5373

Answers (1)

Marko Topolnik
Marko Topolnik

Reputation: 200158

Your code seems fine. Two things currently occur to me as theoretically possible problems:

  1. A file permission problem. Maybe place the file somewhere else than the drive root and check explicitly its permissions. You may call canRead() directly from your code -- that's what ImageIO.read does. It must return true.

  2. There could be some whitespace at the end of your path argument. Use trim() to eliminate that possibility.

Upvotes: 2

Related Questions