New to Java? We'll help you get started with our revised beginner's tutorial, or our free online textbook.
|
Get the latest Java books |
|
h t t p : / /w w w . j a v a c o f f e e b r e a k . c
o m /
|
Menu Articles Using Java Applets Looking for Java resources? Check out the Java Coffee Break directory! |
"Why is my applet gray?"
|
Curious? |
They occur when an error in the loading or execution of an applet has occurred, causing an applet crash. In Java, the technical term for these errors is an Exception. Good Java applets will "catch" these exceptions, or at least attempt to catch the most likely of errors. Many though are unforeseen, and some not even the fault of the applet. Let's look at some of the most obvious causes.
The causes for applet exceptions are numerous, but some of the most common are: -
Some applets are composed of a single class, while others are made up of a large number of classes. Each class must be fetched individually, and if a network fault occurs during the loading of any of these classes, an exception will be thrown. These exceptions occur before the applet even fully loads, and so are hard to guard against.
The easiest way though to avoid these errors is to package your applet into a .ZIP, .JAR, or .CAB archive. Netscape supports .ZIP and .JAR archives, while .CAB is specific to Internet Explorer.
Networking problems are very frustrating. Sometimes a networking host is down, and so applets can't establish network connections to it. Other times, the user is behind a firewall, and so Socket or DatagramSocket requests will not work. Please, if you write networking applets, consider using HTTP for communication rather than TCP/UDP, as the number of people behind firewalls is growing.
Another cause of problems are networking applets that violate browser security restrictions. They run fine in Appletviewer, and then fail in Netscape / Internet Explorer. An applet can only connect to the host from which it is loaded.
Many applets also have problems when looking up IP addresses, so specify the CODEBASE of the applet and the host it will connect to as an IP address, rather than a hostname.
Null pointer exceptions are another of the most common types of exceptions that crash applets. Unfortunately, this one is usually a bug in the applet code. When assigning an object reference to a variable, be sure to check that it is not null.
For example, consider the following code: -
// Get an applet parameter String count = getParameter ("count"); if (count == null) count = "1";
Notice the guard statement, which assigns a default value of no parameter was specified. There are plenty of other places where null pointers can creep in - be careful to check for them.
It can be frustrating for users to see the Ugly Gray Box of Java - they think that they're somehow at fault. The UGBoJ is equally annoying for developers. With a little care though, you can guard against the more common causes, and help reduce the number of gray applets on the web!
Copyright 1998, 1999, 2000 David Reilly
|
Privacy | Legal | Linking | Advertise! |
Last updated:
Monday, June 05, 2006
|