wood burning stoves 2.0*
The moose likes Java in General and the fly likes uncatchable exceptions when loading images Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "uncatchable exceptions when loading images" Watch "uncatchable exceptions when loading images" New topic
Author

uncatchable exceptions when loading images

Chris James
Greenhorn

Joined: Sep 07, 2005
Posts: 19
Hi

I am writing an applet which displays random images, should be simple enough but I have run into a couple of problems

The problem is if I load an image an exception is thrown this ONLY happens when the image is the result of a redirection AND the applet is loaded by a browser.

If I load the applet in the IDE (jbuilderX) the applet performs as expected with no errors.

Two issues arise from this...

1/ Why is an exception being thrown ?
The URL used to load the image is a script that randomly redirects to an existing image on the same server. so no security issues etc...

2/ Because the exception is thrown in another thread (assumed) I cannot catch it. therefore my applet can't recover from it. furthermore the MediaTracker object used to track the image is also unaware that an error has occured. and when queried simply returns that the image has not loaded yet.


Here is the thrown exception as caught by the console....

Java Plug-in 1.5.0_04
Using JRE version 1.5.0_04 Java HotSpot(TM) Client VM
User home directory =

Uncaught error fetching image:
java.lang.NullPointerException
at sun.net.www.protocol.http.HttpURLConnection.followRedirect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)



Any clues as to whats going on ?

Regards
Chris
[ September 07, 2005: Message edited by: Chris Topher ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
It's hard to say with what you've given. I made this up and tested it on a server; seems to work okay.
Chris James
Greenhorn

Joined: Sep 07, 2005
Posts: 19
Hi Craig


The problem only occurs if the server redirects to another URL.
e.g.
applet.getImage(URL("http://www.someplace.com/cgi-bin/randomimage.pl"));
randomimage.pl redirects the caller to an image which for example may be
http://www.someplace.com/images/cat.jpg or
http://www.someplace.com/images/dog.jpg etc.

The example you gave simply chooses an image at random but no redirection is occuring.

The really odd part about it is the error only occurs when the applet is hosted by a browser. When hosted within the IDE there is no issue.

applet.getImage() works asynchronously, in other words to load the image it creates a thread. So if an error occurs in that thread and an exception is thrown it is not catchable. Not that I can work out anyway.

I have since rewritten my random script not to redirect but to instead serve a random image and the applet works fine. so the problem is definately with the redirection part.

What I cannot understand is why the applet works ok in the IDE but throws an exception in the browser.

I am starting to think the problems may be that the IDE and browser use different run time libraries. and that they are fundamentally different.


Chris
[ September 08, 2005: Message edited by: Chris Topher ]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Could you post the working part of the "random script that redirects"?
Is it in a servlet?
I have a HTTPServer that I use for testing and would like to see what happens.
Thanks.
Chris James
Greenhorn

Joined: Sep 07, 2005
Posts: 19
Hi

The script is just a very simple perl script

each time you call it it looks for jpgs in a particular folder

It then selects a random image and then redirects to it

Root ir the domain and folder containing the image
imagefolder is the relative path from this script to the image folder


#!/usr/bin/perl

my $Root = 'http://www.DOMAIN.com/rotate/';
my $imagefolder = '../public_html/rotate';

##########################################################################
# PROGRAM ENTRANCE
Main;


# This is the program's main function
sub Main {
if (opendir (DIR, "$imagefolder"))
{
my @Pics = grep {/.+\.jpg$/i} readdir DIR; #
my $r = int(rand($#Pics));
my $banner = $Pics[$r];
Redirect("$Root$banner");
}
else
{
print "Status: 500 server error\r\n";
print "content-type: text/plain\r\n\r\n";
print "$!";
}
}


sub Redirect {

print <<redirectend;
Cache-Control: no-cache
Pragma: no-cache
Location: $_[0]
URI: $_[0]

redirectend
}
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Thanks, I'll give it a try when I get a chance.

Have you tried referencing the .pl from an HTML <IMG SRC= statement?
Chris James
Greenhorn

Joined: Sep 07, 2005
Posts: 19
Yes

I have been using that script for years and know it works

also the applet works as expected when hosted in Borlands JbuilderX IDE it only fails when its hosted in a browser

the applet was built using the java 1.4 development kit
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
I tried referencing the .pl from an html page via
<IMG SRC="cgi-bin/RandomImage.pl">
It failed with the .pl file you posted.

I added: HTTP/1.0 301 OK
to be the first record returned in the HTTP Response header and it worked.
Chris James
Greenhorn

Joined: Sep 07, 2005
Posts: 19
Interesting

What server are you using? my apache server adds
HTTP/1.1 302 Found

perhaps its the http/1.1 that is causing the problem.

A bt odd at the very least one would expect the java runtime to be http1.1 compliant. its been around for a long time now

normally its left up to the server to add the status string. at least I have always done so. never had a problem before?
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
My server is home grown.

How does the server know what status code to put in the response header? What if the script was returning text vs doing a redirect?

I think that the browser as the AppletContext is what processes the getImage() method. I don't think the Java runtime is involved in processing the redirect. Try writting a test applet that uses Sockets and see what it gets back.
The other solution is to change the handshake between the applet and perl script - have the script return the URL as text and then have the applet issue the getImage() with that URL.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Forget that last bit about the AppletContext. I reread the original question and see that the error was in JVM code:
at sun.net.www.protocol.http.HttpURLConnection.followRedirect(Unknown Source)

Am now trying to decompile that class to see what it's up to. Current version of my decompiler unable to read the file although a byte code viewer is able to read it. ???

Does your applet work with the 1.4 JRE plugin?
Chris James
Greenhorn

Joined: Sep 07, 2005
Posts: 19
Home grown. you are clever

Apache adds the header in after the content has been sent to it.

the only header you must add is the content-type header. the rest it discerns from what its got. (makes a good guess)

If you want to send a specific header then you add it yourself as my script shows in the case of an error. It adds the status: 500 server error line

My windows runtime is 1.5 but my development jdk is 1.4 I assume the browser uses 1.5 and the ide uses 1.4

perhaps thats why it works in the IDE yet fails in the browser.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: uncatchable exceptions when loading images
 
Similar Threads
Applet InvocationTargetException
Applet in JSP
images not loading
Obsolete Interface Used, a bug of JRE?
"Bad Magic Number" Loading Applet/Portlet