JDK-4133573 : BoxView.paint() causes NullPointerException when printing
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 1998-04-29
  • Updated: 1999-09-17
  • Resolved: 1999-09-17
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.3.0 kestrelFixed
Related Reports
Relates :  
Description
Date: Wed, 15 Apr 1998 17:18:17 -0700
To: ###@###.###
From: "Co H. Hoang" <###@###.###>
Subject: bug?

I am trying to write a test to see how printing works and run into this 
NullPointerException.  It looks like BoxView.paint() didn't check to
see if clip is null before passing it to intersects().  Is this going to be
fixed and when?  

I have attached the 2 files printJFC.java and HtmlPanel.java that I used
for my test.  Do 'java printJFC' to run, and type an URL into the text field,
wait for the page to render and click on print.

Thanks,
Co


Exception occurred during event dispatching:
java.lang.NullPointerException:
        at java.awt.Rectangle.intersects(Rectangle.java:383)
        at com.sun.java.swing.text.BoxView.paint(BoxView.java:154)
        at com.sun.java.swing.text.html.HTMLRootView.paint(HTMLRootView.java:106
)
        at com.sun.java.swing.text.DefaultTextUI$RootView.paint(DefaultTextUI.ja
va:741)
        at com.sun.java.swing.text.DefaultTextUI.paintSafely(DefaultTextUI.java:
335)
        at com.sun.java.swing.text.DefaultTextUI$SafePainter.run(DefaultTextUI.j
ava:999)
        at com.sun.java.swing.text.AbstractDocument.render(AbstractDocument.java
:249)
        at com.sun.java.swing.text.DefaultTextUI.paint(DefaultTextUI.java:442)
        at com.sun.java.swing.plaf.ComponentUI.update(ComponentUI.java:47)
        at com.sun.java.swing.JComponent.paintComponent(JComponent.java:374)
        at com.sun.java.swing.JComponent.paint(JComponent.java:579)
        at java.awt.Component.print(Component.java:1377)
        at java.awt.Container.print(Container.java:760)
        at HtmlPanel.print(HtmlPanel.java:39)
        at printJFC.actionPerformed(printJFC.java:74)

----------------------------------------------
// printJFC.java
import com.sun.java.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class printJFC extends JApplet implements ActionListener, KeyListener
{
	JFrame frame = new JFrame("test");
	JButton button = new JButton("print");
	JTextField textField = new JTextField();
	HtmlPanel htmlPanel = new HtmlPanel();

	public printJFC()
	{
		init();
	}

	public void init()
	{
		frame.getContentPane().setLayout(
			new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
		frame.getContentPane().add(textField);
		frame.getContentPane().add(htmlPanel);
		frame.getContentPane().add(button);
		button.addActionListener(this);
		textField.addKeyListener(this);
		frame.setSize(800, 600);
		frame.setVisible(true);

	}

	public void keyTyped(KeyEvent e)	{}
	public void keyPressed(KeyEvent e) {}

	public void keyReleased(KeyEvent event)
	{
		if (KeyEvent.VK_ENTER == event.getKeyCode())
		{
			try
			{
				htmlPanel.gotoURL(new URL(textField.getText()));
			}
			catch (MalformedURLException e)
			{
				System.out.println("MalformedURLException caught in keyReleased()!");
			}
		}
	}

	public void actionPerformed(ActionEvent event)
	{
		if (event.getSource() == button)
		{
			PrintJob pj = frame.getToolkit().getPrintJob(frame, "currentURL", null);
			if (pj != null) 
			{
				Graphics g = pj.getGraphics();

				htmlPanel.print(g);
				pj.end();
			}
		}
	}

	public static void main(String args[])
	{
		new printJFC();
	}

}

--------------------------------------------
//HtmlPanel.java
import com.sun.java.swing.*;
import com.sun.java.swing.border.*;
import com.sun.java.swing.event.*;
import com.sun.java.swing.text.*;
import com.sun.java.accessibility.*;
import java.awt.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
public class HtmlPanel extends JPanel implements HyperlinkListener 
{
	JEditorPane html;
	public HtmlPanel() 
	{
		setBorder(new EmptyBorder(10,10,10,10));
		setLayout(new BorderLayout());
		getAccessibleContext().setAccessibleName("HTML panel");
		getAccessibleContext().setAccessibleDescription(
			"A panel for viewing HTML documents, and following their links");
		html = new JEditorPane();
		html.setEditable(false);
		html.addHyperlinkListener(this);

		JScrollPane scroller = new JScrollPane();
		scroller.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
		
		JViewport vp = scroller.getViewport();
		vp.add(html);
		vp.setBackingStoreEnabled(true);
		add(scroller, BorderLayout.CENTER);
	}

	public void print(Graphics g)
	{
		html.print(g);
	}

	public void gotoURL(URL url)
	{
		try
		{
			html.setPage(url);
		}
		catch (IOException ioe)
		{
			System.out.println("IOException caught in gotoURL(), url = " + url);
		}
	}

	public void hyperlinkUpdate(HyperlinkEvent e) 
	{
		if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) 
			linkActivated(e.getURL());
	}
	protected void linkActivated(URL u) 
	{
		Cursor c = html.getCursor();
		Cursor waitCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
		html.setCursor(waitCursor);
		SwingUtilities.invokeLater(new PageLoader(u, c));
	}
	class PageLoader implements Runnable 
	{
		URL url;
		Cursor cursor;

		PageLoader(URL u, Cursor c) 
		{
			url = u;
			cursor = c;
		}
		public void run() 
		{
			if (url == null) 
			{
				// restore the original cursor
				html.setCursor(cursor);
				// PENDING(prinz) remove this hack when 
				// automatic validation is activated.
				Container parent = html.getParent();
				parent.repaint();
			} 
			else 
			{
				Document doc = html.getDocument();
				try 
				{
					html.setPage(url);
				} 
				catch (IOException ioe) 
				{
					html.setDocument(doc);
					getToolkit().beep();
				} 
				finally 
				{
					// schedule the cursor to revert after
					// the paint has happended.
					url = null;
					SwingUtilities.invokeLater(this);
				}
			}
		}  // void run()
	}  // class PageLoader
}


=========================================================
Co Hoang                        Extensity, Inc.
mailto:###@###.###     2200 Powell St., Ste. 400
http://www.extensity.com        Emeryville, CA 94608
(510)594-5706                   Fax: (510)596-2676


Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: swing1.1 FIXED IN: kestrel INTEGRATED IN: kestrel
14-06-2004

EVALUATION BoxView checks to see if the children are unclipped before trying to render them. It doesn't consider the possibility of a null clipping rectangle however, and it should. This is the only problem however, the print method on JTextComponent should be reimplemented to at least do a minimal job of supporting printing out the content. timothy.prinzing@eng 1998-05-28 Currently I can print text views with no problem on 1.3. This bug has become quite stale. Although there were problems, it works fine on 1.3 so I'm closing this bug out.
28-05-1998