-
Bug
-
Resolution: Fixed
-
P3
-
1.4.0
-
beta
-
generic
-
generic
Name: krC82822 Date: 12/10/2000
orig synopsis: "Opaque JLayeredPane can throw NullPointerException from print() and paint()"
10 Dec 2000, eval1127@eng -- same behavior in merlin(1.4) beta44
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0beta-b44)
Java HotSpot(TM) Client VM (build 1.4beta-B44, mixed mode)
java version "1.3.0beta_refresh"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0beta_refresh-b09)
Java HotSpot(TM) Client VM (build 1.3.0beta-b07, mixed mode)
Also JDK1.3 for Windows and JDK1.3FCS for Linux
When an opaque JLayeredPane is passed a Graphics object which hasn't had a
clipping rectangle set, the JLayeredPane will throw a NullPointerException from
either the print(Graphics g) or paint(Graphics g) methods.
/* Following code will throw a NullPointerException. */
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
public class JLayeredPaneBug {
// Prints a JLayeredPane to an image.
public static void main(String[] args) {
// Create frame to hold JLayeredPane.
JFrame frame = new JFrame();
// Create an opaque JLayeredPane.
JLayeredPane pane = new JLayeredPane();
Dimension size = new Dimension(100,100);
pane.setPreferredSize(size);
pane.setOpaque(true);
// Add JLayeredPane to JFrame and make the frame visible.
frame.setContentPane(pane);
frame.pack();
frame.setVisible(true);
// Create a compatible image from the JLayeredPane, get the
// graphics context for this image, and print the JLayeredPane
// into the image.
Image image = pane.createImage(size.width,size.height);
Graphics g = image.getGraphics();
pane.print(g);
}
}
The exact trace for the error message is:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.JLayeredPane.paint(JLayeredPane.java:544)
at JLayeredPaneBug.main(JLayeredPaneBug.java:30)
The problem is in the paintComponent method of JLayeredPane.
/* Code from JLayeredPane lines 532-547.*/
/**
* Paints this JLayeredPane within the specified graphics context.
*
* @param g the Graphics context within which to paint
*/
public void paint(Graphics g) {
if(isOpaque()) {
Rectangle r = g.getClipBounds(); // CAN RETURN NULL HERE!
Color c = getBackground();
if(c == null)
c = Color.lightGray;
g.setColor(c);
g.fillRect(r.x, r.y, r.width, r.height); // ERROR IF r==NULL!
}
super.paint(g);
}
Specifically, NULL is a valid return from the getClipBounds method. If NULL is
actually returned the fillRect method will generate a NullPointerException. One
possibility is to replace the getClipBounds() by getBounds().
(Review ID: 110821)
======================================================================