United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 6653511 JComponent.safelyGetGraphics() may sometimes return null
6653511 : JComponent.safelyGetGraphics() may sometimes return null

Details
Type:
Bug
Submit Date:
2008-01-22
Status:
Closed
Updated Date:
2011-04-06
Project Name:
JDK
Resolved Date:
2011-04-06
Component:
client-libs
OS:
generic
Sub-Component:
javax.swing
CPU:
generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
7
Fixed Versions:
7

Related Reports
Relates:

Sub Tasks

Description
The JComponent.safelyGetGraphics() is widely used in javax.swing to obtain the Graphics object for a component. This method uses the Component.getGraphics() to obtain one. The javadoc for getGraphics() states that this method may sometimes return null. However, Swing code never verifies the return value of this method, and therefore may throw NPE under some circumstances.

One of the cases when this problem is observed is the test/closed/java/awt/Container/ZOrder/ tets. When ran with JDK7b18 (or later, and probably some older releases) it constantly throws NPE at the JComponent._paintImmediately() method when trying to do g.dispose(), and g is null (as previously returned by the safelyGetGraphics()).

                                    

Comments
EVALUATION

we should add a null check
                                     
2011-02-28
SUGGESTED FIX

@@ -5094,11 +5094,11 @@
                 }
             }
         }
 
         try {
-            g = safelyGetGraphics(paintingComponent, c);
+            if ((g = safelyGetGraphics(paintingComponent, c)) != null) {
             try {
                 if (hasBuffer) {
                     RepaintManager rm = RepaintManager.currentManager(
                                                bufferedComponent);
                     rm.beginPaint();

@@ -5119,10 +5119,11 @@
                 }
             } finally {
                 g.dispose();
             }
         }
+        }
         finally {
             // Reset the painting child for the parent components.
             if(paintingComponent != this) {
                 Component comp;
                 int i = pIndex;
                                     
2008-01-23



Hardware and Software, Engineered to Work Together