Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Race-condition bugs (4/20/99)

The JavaWorld experts answer your most pressing Java questions -- every week

  • Print
  • Feedback

QWhy does the following code generate a NullPointerException?

 
public class SimpleApplet extends java.applet.Applet {
   java.awt.Image art;
   public void init() {
      art = getImage(getDocumentBase(), getParameter("img"));
   }
   public void paint(java.awt.Graphics g) {
      g.drawImage(art, 0, 0, this);
   }
}


A Because art is null.

There is a race-condition (threading) bug in some older applet environments (the JVM and the browser) such that an Applet's paint() method can be called before its init() method. The workaround (aside from updating your environment) is to add if (art != null) before the call to drawImage(). Once init() is called, the subsequent calls to paint() will work properly.

Of course, you'll need to make sure that SimpleApplet can find the image file; make sure getDocumentBase() returns a valid URL, and that the HTML page is set up correctly (so that getParameter() returns a valid image-file name).

About the author

Random Walk Computing is the largest Java/CORBA consulting boutique in New York, focusing on solutions for the financial enterprise. Known for their leading-edge Java expertise, Random Walk consultants publish and speak about Java in some of the most respected forums in the world.
  • Print
  • Feedback

Resources