in the following patch you can see that i added a check for an array index out of bounds and a check for a null pointer. it is quite possible that the two methods i fixed state in their pre-conditions that something like that should not happen, but then the bug is somewhere else because these pre-conditions were violated. Index: src/java/org/apache/poi/hssf/record/UnknownRecord.java =================================================================== RCS file: /home/cvspublic/jakarta-poi/src/java/org/apache/poi/hssf/record/UnknownRecord.java,v retrieving revision 1.9 diff -c -r1.9 UnknownRecord.java *** src/java/org/apache/poi/hssf/record/UnknownRecord.java 9 Apr 2004 13:05:20 -0000 1.9 --- src/java/org/apache/poi/hssf/record/UnknownRecord.java 30 Jun 2004 17:31:44 -0000 *************** *** 62,68 **** { sid = id; thedata = new byte[size]; ! System.arraycopy(data, offset, thedata, 0, size); } /** --- 62,69 ---- { sid = id; thedata = new byte[size]; ! int len = Math.min(size, data.length - offset); ! System.arraycopy(data, offset, thedata, 0, len); } /** Index: src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java =================================================================== RCS file: /home/cvspublic/jakarta-poi/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java,v retrieving revision 1.27 diff -c -r1.27 HSSFSheet.java *** src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java 9 Apr 2004 13:05:24 -0000 1.27 --- src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java 30 Jun 2004 17:31:44 -0000 *************** *** 308,313 **** --- 308,315 ---- public HSSFRow getRow(int rownum) { + if(null == this.sheet.getRow(rownum)) + return null; HSSFRow retval = new HSSFRow(book, sheet, this.sheet.getRow(rownum)); return retval;
without the change to UnknowRecord.java (i hit this and basically coded the same) i cannot use HSSF to read spreadsheets with pivot tables (and probably pivot charts). so this is a pretty important change to get back to the mothership.
The Excel template when read using HSSF throws the arrayIndexException. Just for kicks when I altered the code in UnknownRecord (Line 62): from System.arraycopy(data, offset, thedata, 0, size); to try { System.arraycopy(data, offset, thedata, 0, size); } catch (Exception e) { // Hack to clear the bug thedata = new byte[data.length - offset]; System.arraycopy(data, offset, thedata, 0, data.length - offset); } things went ahead fine. But I guess this may not be the correct solution even though it worked for me. I can provide a test template if it is of any help (amit_vijayant@yahoo.com).
I believe that this is an invalid fix. If someone has an excel file that generates this exception please REOPEN this bug and attach the file. Sorry cant do much with this limited information. Jason
Created attachment 16158 [details] just load this up in hssf and it will blow up without the suggested fixes
try reading in the attached .xls. unless something has changed it will blow up as described without the fixes described. is that still too 'limited' or do you need a sample program that will invoke it as well?
Using HEAD and the latest CVS snapshot the following works for me: HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File( "c:/TestPivot.xls"))); wb.write(new FileOutputStream(new File("c:/TestPivot_out.xls"))); I can even open and use the out file in excel!! Jameison can you please confirm that the above would have previously failed. Jason
I believe that this has been fixed in HEAD. Based on the earlier comments it would explode on read, now it will read and write the workbook without problems. Jason