Bug 26916 - ArrayIndexOutOfBoundsException in HSSFDataFormat.getBuiltinFormat
Summary: ArrayIndexOutOfBoundsException in HSSFDataFormat.getBuiltinFormat
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 2.0-dev
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2004-02-13 15:53 UTC by Brian J. Sayatovic
Modified: 2006-07-22 23:49 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description Brian J. Sayatovic 2004-02-13 15:53:34 UTC
While attempting to read a workbook with skeletal test program, I encountered 
the following exception:

java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 191
	at java.util.Vector.get(Vector.java:716)
	at org.apache.poi.hssf.usermodel.HSSFDataFormat.getBuiltinFormat

The portion of my code which lead up to this was inside a double loop over each 
row, and each cell in the row:

	HSSFCellStyle style = cell.getCellStyle();
	short format = style.getDataFormat();
	String formatString = HSSFDataFormat.getBuiltinFormat(format);

The way I read the documentation, the HSSFDataFormat.getBuiltinFormat call 
should return a format String if one exists, or null.  In this case, however, 
neither happened.  Instead, I got an ArrayIndexOutOfBoundsException.

Unless I've missed something, I suspect either the documentation should be 
updated or the code altered.  For now, I'm going to attempt to work around this 
by checking the format index against 
Comment 1 Avik Sengupta 2004-02-13 19:08:52 UTC
It will be much easier to fix if you could possibly supply a small worksheet and
a test program that replicates this error.
Comment 2 Jason Height 2006-01-13 10:45:52 UTC
Cant fix without excel file or better example. Sorry.

Comment 3 Brian J. Sayatovic 2006-01-13 13:48:25 UTC
I know longer have my original file, but looking at the 2.0 source, the problem 
is easy to see:

308    /**
309     * get the format string that matches the given format index
310     * @param index of a built in format
311     * @return string represented at index of format or null if there is not 
a builtin format at that index
313     */
314    public static String getBuiltinFormat( short index )
315    {
316        if ( builtinFormats == null )
317        {
318            populateBuiltinFormats();
319        }
320        return (String) builtinFormats.get( index );
321    }

The JavaDoc clearly states that a String will be returned, or if one doesn't 
exist at that index, null will be returned.  The implementation, however, has 
no guard to make sure its a valid index.  All that has to be done is to check 
to see if index is less than the size of the list before calling get(index).  
If the index is greater-than-or-equal-to the size of the list, then null should 
be returned.

There is a chance that I'm misunderstanding the intent of this method from the 
JavaDoc.  If that is the case, I suggest the JavaDoc be changed to clarify that 
it is possible for this method to throw an exception (instead of returning 
null) when a bad index is given.
Comment 4 Jason Height 2006-07-23 06:49:15 UTC
Ahh ok so it is a documentation type issue. Quite obvious when pointed out.
Indicated possible exception in javadoc.