JDK-6653511 : JComponent.safelyGetGraphics() may sometimes return null
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2008-01-22
  • Updated: 2011-04-06
  • Resolved: 2011-04-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b134Fixed
Related Reports
Relates :  
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
28-02-2011

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;
23-01-2008