article 09





java.lang.NullPointerException thrown from a thread ...






Documentation Contents
p, li {font-family: arial; font-size: 10pt;}



java.lang.NullPointerException Thrown From
a Thread When Applet.start() or Applet.stop() is
Called

Symptoms

When running an applet in a browser using the Sun JavaTM Runtime Environment (JRETM), a java.lang.NullPointerException
is thrown from a thread when Applet.start() or Applet.stop()
is called. The same applet runs under the Microsoft Virtual Machine (VM).

Cause

In the Microsoft VM, an applet may be visible immediately after Applet.init()
is called but before Applet.start(). Similarly, an applet may
still be visible after Applet.stop() but before Applet.destroy().
The Sun JRE implementation is slightly different in terms of applet visibility
related to the lifecycle methods, so any applet that relies on the Microsoft
VM event sequences may break.

Resolution

Code defensively to ensure the applet is visible before drawing. One way
to do this is to test whether the Graphics object is null.
For example, suppose the applet has the following methods:
        private Thread paintThread
= null;

        public void start()
        {
               
if (paintThread == null)
               
{
                   
paintThread = new Thread(this);
                   
paintThread.start();           

               
}   
        }
        public void stop()
        {
               
if (paintThread != null)
               
{
                   
paintThread.interrupt();    // Assuming the thread
has sufficient                                       //
time to be interrupted and stop                                       //
drawing before destroy() is called.
                   
paintThread = null;
               
}
        }

        public void run()
        {
               
while (Thread.currentThread().isInterrupted() == false)
               
{
                       
Graphics g = getGraphics();                   

                       
g.drawString("Hello World", 100, 100);   //
java.lang.NullPointerException if                                                      //
applet is not visible -
                                                     //
g will be null !
               
}
        }

        The code for the run
method should be changed to the following:
        public void run()
        {
               
while (Thread.currentThread().isInterrupted() == false)
               
{
                       
Graphics g = getGraphics();                 

                       
if (g != null)               //
Code defensively!
                           
g.drawString("Hello World", 100, 100);  

               
}
        }

Related Information
        None.
 





Copyright © 1995-2010 Sun Microsystems, Inc. All Rights Reserved. Please send comments using this Feedback page.

Java Technology








Wyszukiwarka

Podobne podstrony:
Bulletin article 6$ 09
pref 09
amd102 io pl09
2002 09 Creating Virtual Worlds with Pov Ray and the Right Front End
Analiza?N Ocena dzialan na rzecz?zpieczenstwa energetycznego dostawy gazu listopad 09
2003 09 Genialne schematy
09 islam
GM Kalendarz 09 hum
06 11 09 (28)

więcej podobnych podstron