JDK-5063236 : 1.4.2_03: TextComponent.setHighlighter(null) & color attr throws NPE GlyphView
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.4.2_03
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_1
  • CPU: sparc
  • Submitted: 2004-06-15
  • Updated: 2004-06-15
  • Resolved: 2004-06-15
Related Reports
Duplicate :  
Description
Seems to be a regression bug in GlyphView 1.4.2_03-b02 where if you set the highlighter to null in a TextComponent and set the color attribute of the text, it will throw the following NullPointerException when trying to render the text.  Sample code that throws this NPE follows the stacktrace.

java.lang.NullPointerException
        at javax.swing.text.GlyphView.paint(GlyphView.java:367)
        at javax.swing.text.BoxView.paintChild(BoxView.java:144)
        at javax.swing.text.BoxView.paint(BoxView.java:407)
        at javax.swing.text.BoxView.paintChild(BoxView.java:144)
        at javax.swing.text.BoxView.paint(BoxView.java:407)
        at javax.swing.text.ParagraphView.paint(ParagraphView.java:569)
        at javax.swing.text.BoxView.paintChild(BoxView.java:144)
        at javax.swing.text.BoxView.paint(BoxView.java:407)
        at javax.swing.plaf.basic.BasicTextUI$RootView.paint(BasicTextUI.java:1319)
        at javax.swing.plaf.basic.BasicTextUI.paintSafely(BasicTextUI.java:636)
        at javax.swing.plaf.basic.BasicTextUI.paint(BasicTextUI.java:770)
        at javax.swing.plaf.basic.BasicTextUI.update(BasicTextUI.java:749)
        at javax.swing.JComponent.paintComponent(JComponent.java:541)
        at javax.swing.JComponent.paint(JComponent.java:808)
        at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4787)
        at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4740)
        at javax.swing.JComponent._paintImmediately(JComponent.java:4685)
        at javax.swing.JComponent.paintImmediately(JComponent.java:4488)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:410)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:117)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
        at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)



--------- Test code -----------
The following throws NPE when run using 1.4.2_03-b02 
Comment out one of the lines 'text.setHighlighter(null); ' or 'StyleConstants.setForeground(attrs, Color.blue);' and it will execute correctly.

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

public class HighlighterTest extends JPanel {

    public static void main(String [] parms) {

	JFrame frame = new JFrame("Test");
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
	JTextComponent text = new JTextPane();
	  // highlighter disabled

	try {
	    SimpleAttributeSet attrs = new SimpleAttributeSet();
	    StyleConstants.setForeground(attrs, Color.blue);
	    text.getDocument().insertString(0, "This is a test", attrs);
	} catch (Exception ex) {
	    ex.printStackTrace();
	    System.exit(10);
	}

	Container cont = frame.getContentPane();
	cont.setLayout(new BorderLayout());
	cont.add(text, BorderLayout.CENTER);
	frame.pack();
	frame.show();
    }
}

Comments
WORK AROUND Instead of setting the highlighter to null, use a "dummy" highlighter that extends javax.swing.text.DefaultHighlighter but has no functionality. E.g.,: /** An implementation of Highlighter that does nothing when called. Used to disable highlighting. **/ class DummyHighlighter extends javax.swing.text.DefaultHighlighter { /** An empty array of Highlighter.Highlight **/ Highlighter.Highlight[] emptyHighlights = {}; /** @inheritDoc @return @inheritDoc **/ public Highlighter.Highlight[] getHighlights() { // System.out.println("DummyHighlighter.getHighlights"); return emptyHighlights; } /** @inheritDoc @param g @inheritDoc **/ public void paint(java.awt.Graphics g) { // System.out.println("DummyHighlighter.paint"); // do nothing } /** @inheritDoc @param p0 @inheritDoc @param p1 @inheritDoc @param p @inheritDoc @return @inheritDoc **/ public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter p) throws BadLocationException { // System.out.println("DummyHighlighter.addHighlight"); // do nothing return null; } /** @inheritDoc @param tag @inheritDoc **/ public void removeHighlight(Object tag) { // System.out.println("DummyHighlighter.removeHighlight"); // do nothing } /** @inheritDoc **/ public void removeAllHighlights() { // System.out.println("DummyHighlighter.removeAllHighlights"); // do nothing } /** @inheritDoc @param tag @inheritDoc @param p0 @inheritDoc @param p1 @inheritDoc **/ public void changeHighlight(Object tag, int p0, int p1) throws BadLocationException { // System.out.println("DummyHighlighter.changeHighlight"); // do nothing } }
18-06-2004

SUGGESTED FIX The line in Glyphview that throws the NPE is the following. Highlighter.Highlight[] h = tc.getHighlighter().getHighlights(); Guard against a null highlighter being returned by tc.getHighlighter()
18-06-2004

PUBLIC COMMENTS 1.4.2_03: TextComponent.setHighlighter(null) & setting the color attribute on text in a StyledDocument throws NPE in GlyphView
18-06-2004

EVALUATION This bug was fixed in 1.5 by the fix for 4532590 [JTextPane jTextPane.setHighlighter(null) doesn't disable highlighter.] Closing as dup If this bug has to be fixed in 1.4.x feel free to reopen it. ###@###.### 2004-06-15
15-06-2004