Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I catch this exception

0 views
Skip to first unread message

Paul Tomblin

unread,
Aug 23, 2002, 10:36:43 AM8/23/02
to
If I do the following:

// filename is a String == "patch.jpg"
final Toolkit toolKit = Toolkit.getDefaultToolkit();
Image tmpImage = toolKit.createImage(filename);
toolKit.prepareImage(tmpImage, -1, -1, this);

and the image wasn't found, I get an error returned to my imageUpdate
method and that's all I hear about it and I can go to plan B (using a
regular background). But if I want to get the image out of a jar file
instead of the current directory, I change that to:

final Toolkit toolKit = Toolkit.getDefaultToolkit();
URL imgURL = getClass().getResource("/"+filename);
Image tmpImage = toolKit.createImage(imgURL);
toolKit.prepareImage(tmpImage, -1, -1, this);

And it works the same if the file is there in the jar file, but if it
isn't, I get the following exception appear on the screen:

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:251)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:168)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)

And I can't seem to catch the exception anywhere. Is there a way to test
if the imgURL exists?

--
Paul Tomblin <ptom...@xcski.com>, not speaking for anybody
Don't use a big word where a diminutive one will suffice.

Vincent

unread,
Aug 23, 2002, 12:49:58 PM8/23/02
to

"Paul Tomblin" <ptom...@xcski.com> wrote in message
news:ak5h9r$shp$1...@allhats.xcski.com...

Did you try "if (imgURL !=null) { ...}"?


Paul Tomblin

unread,
Aug 23, 2002, 1:01:36 PM8/23/02
to
In a previous article, "Vincent" <vlasf...@yahoo.com> said:
>"Paul Tomblin" <ptom...@xcski.com> wrote in message
>news:ak5h9r$shp$1...@allhats.xcski.com...
>> URL imgURL = getClass().getResource("/"+filename);
>
>Did you try "if (imgURL !=null) { ...}"?

Oh man, do I feel stupid! Of course that works. I was confusing it with
something like File("filename"), where just creating it doesn't tell you
if it actually exists or not.

Sorry to waste everybody's time.

--
Paul Tomblin <ptom...@xcski.com>, not speaking for anybody

A bleeding heart can be hell on the carpeting.

0 new messages