You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Neil Aggarwal <ne...@JAMMConsulting.com> on 2008/03/19 23:25:07 UTC
Suggested fix for Illegal length exception when trying to read Excel sheet
Hello all:
I traced through the POI source code.
The problem is happening when fillFields(RecordInputStream in)
calls this line:
field_5_username = in.readCompressedUnicode(field_3_username_length);
RecordInputStream has this condition at the top of
readCompressedUnicode(int length)
if ((length < 0) || ((remaining() < length) && !isContinueNext())) {
throw new IllegalArgumentException("Illegal length");
}
In this instance, length is zero, remaining() returns -1, and
isContinueNext() returns false. That make the if statement fire
the exception.
If the length is zero, we should return an empty string. So,
if I add this check to the top of the method:
if( length <= 0 )
return "";
Everything works fine.
I think this is a pretty simple patch so I added it to the
bugzilla:
https://issues.apache.org/bugzilla/show_bug.cgi?id=44643
Thanks,
Neil
--
Neil Aggarwal, (832)245-7314, www.JAMMConsulting.com
Eliminate junk email and reclaim your inbox.
Visit http://www.spammilter.com for details.
> -----Original Message-----
> From: Neil Aggarwal [mailto:neil@jammconsulting.com]
> Sent: Wednesday, March 19, 2008 4:40 PM
> To: user@poi.apache.org
> Subject: Illegal length exception when trying to read Excel sheet
>
> Hello:
>
> This is really strange. I have two empty spreadsheets. POI
> can read one
> but not the other.
>
> When I try to read the sheet.xls file, I get this exception:
>
> Exception in thread "main"
> org.apache.poi.hssf.record.RecordFormatException:
> Unable to construct record instance
> at
> org.apache.poi.hssf.record.RecordFactory.createRecord(RecordFa
> ctory.java:199
> )
> at
> org.apache.poi.hssf.record.RecordFactory.createRecords(RecordF
> actory.java:11
> 7)
> at
> org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook
> .java:207)
> at
> org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook
> .java:259)
> at
> org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook
> .java:240)
> at POITest.main(POITest.java:9)
> Caused by: java.lang.reflect.InvocationTargetException
> at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
> Method)
> at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown
> Source)
> at
> sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown
> Source)
> at java.lang.reflect.Constructor.newInstance(Unknown Source)
> at
> org.apache.poi.hssf.record.RecordFactory.createRecord(RecordFa
> ctory.java:187
> )
> ... 5 more
> Caused by: java.lang.IllegalArgumentException: Illegal length
> at
> org.apache.poi.hssf.record.RecordInputStream.readCompressedUni
> code(RecordInp
> utStream.java:270)
> at
> org.apache.poi.hssf.record.FileSharingRecord.fillFields(FileSh
> aringRecord.ja
> va:62)
> at org.apache.poi.hssf.record.Record.<init>(Record.java:55)
> at
> org.apache.poi.hssf.record.FileSharingRecord.<init>(FileSharin
> gRecord.java:4
> 8)
> ... 10 more
>
> Here is my code:
>
> import java.io.*;
> import org.apache.poi.hssf.usermodel.HSSFWorkbook;
>
> public class POITest {
> public static void main(String[] args) throws Exception{
> System.out.println("Reading sheet 2:");
> new HSSFWorkbook(new FileInputStream("C:\\Tmp\\sheet2.xls"));
> System.out.println("Reading sheet 1:");
> new HSSFWorkbook(new FileInputStream("C:\\Tmp\\sheet.xls"));
> }
> }
>
> I have attached the spreadsheets.
>
> Any ideas what is happening?
>
> Thanks,
> Neil
>
> --
> Neil Aggarwal, (832)245-7314, www.JAMMConsulting.com
> Eliminate junk email and reclaim your inbox.
> Visit http://www.spammilter.com for details.
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org