JDK-6513875 : Zero length string in TextLayout constructor: java.lang.IllegalArgumentException
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2007-01-17
  • Updated: 2011-02-16
  • Resolved: 2007-01-17
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Versione 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
I receive an unexpected IllegalArgumentException, if I try to use Font in this way:

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.font.TextAttribute;

public class UnderlineEntryFieldTest {
   public static void main(String[] unused) throws Exception {
      Map map = new Hashtable();
      map.put(TextAttribute.FAMILY, "courier italic");
      map.put(TextAttribute.SIZE, new Float(28.0));
      map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
      Font font = new Font(map);
      JFrame f = new JFrame("!!!!!!!!!!");
      f.setSize(500, 500);
      f.setLocationRelativeTo(null);
      f.getContentPane().setLayout(new java.awt.GridLayout(1,0));
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JTextField tf = new JTextField();
      tf.setFont(font);
      f.getContentPane().add(tf);
      tf.setText("gianni");
      f.setVisible(true);
   }
}

Note if you remove the 11th row:

map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);

the Exception doesn't occur.

In previous release all works fine.

BstRegards

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
copy, paste and compile the source code in the Description.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
JTextField is correctly shown
ACTUAL -
An exception is returned

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.IllegalArgumentException: Zero length string passed to TextLayout constructor.
        at java.awt.font.TextLayout.<init>(Unknown Source)
        at sun.font.FontDesignMetrics.charsWidth(Unknown Source)
        at javax.swing.text.Utilities.getTabbedTextWidth(Unknown Source)
        at javax.swing.text.Utilities.getTabbedTextWidth(Unknown Source)
        at javax.swing.text.PlainView.getLineWidth(Unknown Source)
        at javax.swing.text.PlainView.calculateLongestLine(Unknown Source)
        at javax.swing.text.PlainView.updateMetrics(Unknown Source)
        at javax.swing.text.PlainView.updateDamage(Unknown Source)
        at javax.swing.text.PlainView.insertUpdate(Unknown Source)
        at javax.swing.text.FieldView.insertUpdate(Unknown Source)
        at javax.swing.plaf.basic.BasicTextUI$RootView.insertUpdate(Unknown Source)
        at javax.swing.plaf.basic.BasicTextUI$UpdateHandler.insertUpdate(Unknown Source)
        at javax.swing.text.AbstractDocument.fireInsertUpdate(Unknown Source)
        at javax.swing.text.AbstractDocument.handleInsertString(Unknown Source)
        at javax.swing.text.AbstractDocument.insertString(Unknown Source)
        at javax.swing.text.PlainDocument.insertString(Unknown Source)
        at javax.swing.text.AbstractDocument.replace(Unknown Source)
        at javax.swing.text.JTextComponent.setText(Unknown Source)
        at UnderlineEntryFieldTest.main(UnderlineEntryFieldTest.java:21)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.font.TextAttribute;

public class UnderlineEntryFieldTest {
   public static void main(String[] unused) throws Exception {
      Map map = new Hashtable();
      map.put(TextAttribute.FAMILY, "courier italic");
      map.put(TextAttribute.SIZE, new Float(28.0));
      map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
      Font font = new Font(map);
      JFrame f = new JFrame("!!!!!!!!!!");
      f.setSize(500, 500);
      f.setLocationRelativeTo(null);
      f.getContentPane().setLayout(new java.awt.GridLayout(1,0));
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JTextField tf = new JTextField();
      tf.setFont(font);
      f.getContentPane().add(tf);
      tf.setText("gianni");
      f.setVisible(true);
   }
}
---------- END SOURCE ----------

Release Regression From : 5.0u9
The above release value was the last known release where this 
bug was not reproducible. Since then there has been a regression.

Comments
EVALUATION This is a duplicate of 6478336 and I have confirmed that the fix for that bug resolves this issue, since its the identical issue. The textfield has a null/empty string and the use of a layout attribute triggers construction of a TextLayout. The fix is in JDK7 b03 and will be in JDK 6 update 2.
17-01-2007