JDK-6699843 : IllegalArgumentException found when using Graphics.drawString( "", 0, 0 )
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2008-05-09
  • Updated: 2011-03-08
  • Resolved: 2011-03-08
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.
JDK 7
7 b28Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_02"
Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode, sharing)

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

A DESCRIPTION OF THE PROBLEM :
Running the attached code causes the attached IllegalArgumentException.

When the following line is removed, the exception is not thrown anymore.
    map.put( TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_ONE_PIXEL );

This seems to be related to Bug #6478336 which was correctly fixed in 6u2(b01).

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached code with JRE 1.6.0_02.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Do nothing. Drawing an empty string should not do anything.
ACTUAL -
The code throws the attached IllegalArgumentException.

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.java2d.SunGraphics2D.drawString(Unknown Source)
	at TestClass.main(TestClass.java:21)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class TestClass {

  public static void main( String args[] ) {
    BufferedImage bi = new BufferedImage( 1, 1, BufferedImage.TYPE_INT_RGB );
    Graphics g = bi.getGraphics();

    Font f = new Font( "Dialog", Font.PLAIN, 12 );
    Map map = new HashMap();
    map.put( TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_LOW_ONE_PIXEL );
    f = f.deriveFont( map );
    g.setFont( f );
    g.drawString( "", 0, 0 );
  }

}

---------- END SOURCE ----------

Comments
EVALUATION Need to check for the empty string before calling the TextLayout constructor.
13-05-2008