Bug 45639

Summary: ArrayIndexOutOfBoundsException in the HSSFSheet.groupColumn
Product: POI Reporter: Anton Sharapov <asharapov>
Component: HSSFAssignee: POI Developers List <dev>
Severity: major    
Priority: P2    
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Linux   

Description Anton Sharapov 2008-08-17 04:08:46 UTC
This is the exception that occurs when i try to group columns on sheet:
java.lang.ArrayIndexOutOfBoundsException: -2
	at java.util.ArrayList.get(ArrayList.java:323)
	at org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate.getColInfo(ColumnInfoRecordsAggregate.java:151)
	at org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate.collapseColInfoRecords(ColumnInfoRecordsAggregate.java:448)
	at org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate.groupColumnRange(ColumnInfoRecordsAggregate.java:492)
	at org.apache.poi.hssf.model.Sheet.groupColumnRange(Sheet.java:1194)
	at org.apache.poi.hssf.usermodel.HSSFSheet.groupColumn(HSSFSheet.java:1596)

example code:
        final HSSFWorkbook wb = new HSSFWorkbook();
        final HSSFSheet sheet = wb.createSheet("test");
        sheet.groupColumn((short)7, (short)9);
        sheet.groupColumn((short)4, (short)12);
        sheet.groupColumn((short)1, (short)15);

Tested on current poi version (poi-3.2-alpha1-20080817)
Comment 1 Olaf Diehl 2008-09-10 06:09:44 UTC
A simple patch is to comment out the performance tuning with fromIdx in ColumnInfoRecordsAggregate.groupColumnRange(short fromColumn, short toColumn, boolean indent). It does not work because the column records are NOT ordered by column no:

// taken from 3.1 FINAL org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate:

    public void groupColumnRange(short fromColumn, short toColumn, boolean indent)
// OD bug fix: records NOT ordered by column no
//                fromIdx = columnIdx - 1; // subtract 1 just in case this column is collapsed later.
Comment 2 Josh Micich 2008-09-11 16:24:52 UTC
fixed in svn 694534.

As you pointed out, the ColumnInfoRecords were not being kept in proper order.  Rather than disable the optimisation, I fixed the logic to keep the records sorted.  There were a few other methods besides groupColumnRange() that relied on proper ordering.

Junit added for groupColumnRange() and collapseColumn()/expandColumn()