I am trying to write an application to be launched via our intranet site using Java Web Start, and I want to store resources such as images in a separate jar file so that they can then be easily re-used in other similar apps. The resource jar file is also stored on a central server and can be accessed via either a network path or an " URL on the web server. Either can be specified in the Class-Path attribute in the manifest of the app's jar file and both work fine as long as the resource jar file is there. However, I'm having trouble handling the case where it can't be found (e.g. if the network connection drops for some reason) and the images can't be loaded.
The method I use to load the images is:
If the image can't be found I want the method to simply return null and my app will either use a spacer or a default icon from the L&F. However when I launch the app (removing the resource jar first so it can't be loaded) I get the following exception and the app hangs before even displaying the GUI:
[tt]
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:99)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:108)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:248)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
[/tt]
I also tried putting an additional exception handler in the AWT event queue (by subclassing [tt]java.awt.EventQueue[/tt]), but it didn't help; I don't think just loading an image generates any AWTEvent anyway.
Any other ideas how I can catch this exception?
The method I use to load the images is:
Code:
import java.awt.*;
import java.net.URL;
...
private Image loadImage(String fileName)
{
Image img = null;
try
{
// Try to load image resource
URL url = this.getClass().getClassLoader().getResource(fileName);
img = Toolkit.getDefaultToolkit().getImage(url);
}
catch(Exception ex)
{
// ignore
}
return img;
}
If the image can't be found I want the method to simply return null and my app will either use a spacer or a default icon from the L&F. However when I launch the app (removing the resource jar first so it can't be loaded) I get the following exception and the app hangs before even displaying the GUI:
[tt]
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:99)
at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:108)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:248)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
[/tt]
I also tried putting an additional exception handler in the AWT event queue (by subclassing [tt]java.awt.EventQueue[/tt]), but it didn't help; I don't think just loading an image generates any AWTEvent anyway.
Any other ideas how I can catch this exception?