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 /

Java Coffee Break


Learning Java

Author Profiles
Talk Java!

Using Java

Discuss Java

Looking for Java resources? Check out the Java Coffee Break directory!

"Why is my applet gray?"
or What causes applets to fail

By David Reilly

Applets are both a revolutionary way to produce dynamic web-based content and a major pain in the neck for developers. In an ideal world pitched by Sun, applets are written once, and run on any computer, any browser, any operating system. This vision has been coined Write Once, Run Anywhere (WORA). What a wonderful dream!

In the real world, experienced by thousands of developers, and hundreds of thousands of users, Java applets are a frustrating headache. When they work, they work great. But when they don't, the problem is hard to diagnose, and even harder to repair. Inconsistencies between browser versions and vendors, operating systems, and user configurations make for plenty of problems. Sure there is the occasional glitch in the GUI, or the slow performance on certain machine/browser configurations. But the problem goes further, to what Java developers and web users dread the most. The Ugly Gray Box of Java.

What doesn't kill us makes us stronger

Provided that your browser installation is stable, it is unlikely that your browser will crash or freeze (if it does repeatedly, reinstalling the browser often solves this problem).  More likely is that you'll see the Ugly Gray Box of Java (almost as frustrating as the Blue Screen of Death in Win 9x).

Here's an example of an applet that is guaranteed to give a UGBoJ. Depending on your browser, you may also see a cryptic message in the browser status bar.

This applet requires a Java enabled browser. But don't bother. It is only going to crash.

Get the source

What causes Ugly Gray Boxes?

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.

Causes for Applet Exceptions

The causes for applet exceptions are numerous, but some of the most common are: -

Failure to locate or load a Java class

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.

Failure to access a network resource

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

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!

Back to main

Copyright 1998, 1999, 2000 David Reilly

Privacy | Legal | Linking | Advertise!

Last updated: Monday, June 05, 2006