JDK-4191142 : Calling paint() on a non-visible component throws NullPointerException
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0,1.2.1
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,solaris_2.6,windows_nt
  • CPU: generic,x86,sparc
  • Submitted: 1998-11-18
  • Updated: 1999-01-11
  • Resolved: 1999-01-11
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.
Other
1.2.2 1.2.2Fixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Description

Name: tb29552			Date: 11/18/98


/*

In JDK 1.1 using Swing1.0.3, we had no trouble creating
an offscreen Image and paint()ing a Swing component
using the Image's graphics object.  Under
JDK1.2/Swing1.1, the same code fails with a
NullPointerException in JComponent.

The following simple program demonstrates the effect.
It creates a JTabbedPane with two tabs, each containing
a single JButton.  When tab "A" is selected and "Button
A" is pressed, the code creates an offscreen image, and
calls paint on "Button B" with the image's graphics
context.

Running this program produces the following output:

C:\> java -Djava.compiler=NONE TabTest
Clicking on aa
Paint bb
Exception occurred during event dispatching:
java.lang.NullPointerException:
        at javax.swing.JComponent.paint(JComponent.java:472)
        at TabTest$2.actionPerformed(TabTest.java:40)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1066)
        at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1101)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:217)
        at java.awt.Component.processMouseEvent(Component.java:3126)
        at java.awt.Component.processEvent(Component.java:2965)
        at java.awt.Container.processEvent(Container.java:987)
        at java.awt.Component.dispatchEventImpl(Component.java:2376)
        at java.awt.Container.dispatchEventImpl(Container.java:1032)
        at java.awt.Component.dispatchEvent(Component.java:2289)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:1944)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:1732)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:1645)
        at java.awt.Container.dispatchEventImpl(Container.java:1019)
        at java.awt.Window.dispatchEventImpl(Window.java:712)
        at java.awt.Component.dispatchEvent(Component.java:2289)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:258)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:68) */
*/

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;

public class TabTest {
    public static void main(String args[]) {
        class DriverFrame extends JFrame {
            public DriverFrame () {
                super();

                addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent event) {
                        dispose();      // free the system resources
                        System.exit(0); // close the application
                    }
                });

                Container c = getContentPane();
                final JButton aa = new JButton("Button A");
                final JButton bb = new JButton("Button B");

                c.setLayout(new java.awt.BorderLayout());
                this.setSize(300, 300);
                JTabbedPane jtp = new JTabbedPane();

                aa.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Clicking on aa");
                        Image img = createImage(100, 100);
                        Graphics g = img.getGraphics();
                        System.out.println("Paint bb");
                        bb.paint(g);
                    }
                });
                jtp.addTab("A", aa);
                jtp.addTab("B", bb);
                c.add(jtp);
            }
        }
        new DriverFrame ().setVisible(true);
    }
}

(Review ID: 42826)
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: generic FIXED IN: 1.2.2 swing2.0alpha INTEGRATED IN: 1.2.2
14-06-2004

EVALUATION In 1.2 it is possible for getClipBounds to return null. JComponent was not checking for this, causing the NPE. This has been fixed. scott.violet 1998-12-03
11-06-2004

PUBLIC COMMENTS In 1.2 it is possible for getClipBounds to return null. JComponent was not checking for this, causing the NPE. This has been fixed. scott.violet 1998-12-03
10-06-2004