Bug 46874 - org.apache.poi.hssf.usermodel.HSSFWorkbook.getCellStyleAt() method calls ...model.Workbook.getExFormatAt() with wrong signature & crashes.
Summary: org.apache.poi.hssf.usermodel.HSSFWorkbook.getCellStyleAt() method calls ...m...
Status: RESOLVED WORKSFORME
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.2-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords: ErrorMessage
Depends on:
Blocks:
 
Reported: 2009-03-18 11:42 UTC by Gerald Mortell
Modified: 2009-03-24 10:00 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gerald Mortell 2009-03-18 11:42:05 UTC
Overview:
---------------------------------------------
org.apache.poi.hssf.usermodel.HSSFWorkbook.getCellStyleAt() method calls the
org.apache.poi.hssf.model.Workbook.getExFormatAt() method with a short, when it requires an integer index.   This results in the code crashing (see stack trace below).


Background -
----------------------------------------------
This is my first time reporting a bug, so I apologize in advance if I'm missing anything.

I am trying to create a java app to read in an Excel spreadsheet & split it out into multiple spreadsheets, based upon key data.   To do this I need to clone the CellStyles from the input workbook to each of the output workbooks, so dates and currency formats will be the same in the output spreadsheets as they are in the input spreadsheet. 

Steps to Reproduce: My Code That Initializes Each Output Workbook -
-------------------------------------------------------------------
//Clone the input workbook's CellStyles into the output workbook's CellStyles...

for (short i = 0 ; i <= inputHSSFWorkbook.getNumCellStyles() ; i++)
{
    LOG.info("Synchronizing input & output workbook CellStyles...i = " +i);
    if (i > numberOfCellStylesOut)
    {   //create a new CellStyle (blank) and add it to the W/B's CellStyle table.
        outputHSSFWorkbook.createCellStyle();
    }

    // Now try to synchronize all of the input & output workbook's CellStyle's.
    // the 'outputHSSFWorkbook.getCellStyleAt(i)' piece below returns the output workbook's
    // HSSFCellStyle object at index i.   Clone it from the input CellStyle at index i.
    outputHSSFWorkbook.getCellStyleAt(i).cloneStyleFrom(inputHSSFWorkbook.getCellStyleAt(i));
}

numberOfCellStylesOut = outputHSSFWorkbook.getNumCellStyles();
LOG.debug("After synchronizing Input & Output CellStyle's - The number of CellStyles in this output workbook is " + numberOfCellStylesOut);

There were 28 CellStyles in the input workbook and 21 in the new output workbook.  I would expect to now see 28 CellStyles in each output workbook

Actual Results: The Stack Trace From the Crash -
------------------------------------------------

    Exception in thread "main" java.lang.ClassCastException: org.apache.poi.hssf.record.StyleRecord
        at org.apache.poi.hssf.model.Workbook.getExFormatAt(Workbook.java:742)
        at org.apache.poi.hssf.usermodel.HSSFWorkbook.getCellStyleAt(HSSFWorkbook.java:1125)



Expected Results:
-----------------------------------------------
I expected the code to clone the CellStyles from the input workbook to each of the output workbooks.


Build Date & Platform: 
-----------------------------------------------
I encountered this on both the 3.2-FINAL build and the 3.5-Beta5 builds.  I think this will only happen on versions 3.2-FINAL and higher, since that is when the cloneStyleFrom() methosd was available.  Here's the MANIFEST.MF file from my POI jar...

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.2
Created-By: 1.5.0_13-b05 (Sun Microsystems Inc.)
Built-By: yegor
Specification-Title: Apache POI
Specification-Version: 3.2-FINAL-20081019
Specification-Vendor: Apache
Implementation-Title: Apache POI
Implementation-Version: 3.2-FINAL-20081019
Implementation-Vendor: Apache


Additional Information - The Apache POI HSSF Code - 
----------------------------------------------------
org.apache.poi.hssf.usermodel.HSSFWorkbook.getCellStyleAt() method passes a short to the 
org.apache.poi.hssf.model.Workbook.getExFormatAt() method, which doesn't have a signature to accept a short.   getExFormatAt()'s signature is for an int.

If the org.apache.poi.hssf.usermodel.HSSFWorkbook.getCellStyleAt() method assigns its input short to an int, it could pass the int in the method call to org.apache.poi.hssf.model.Workbook.getExFormatAt() method.  

I believe that this will work without crashing.   To be honest, I'm not as experienced as most Java developers, so this might not be right.   You folks would know better.

Hope this helps,

Gerald Mortell
Comment 1 Nick Burch 2009-03-24 10:00:08 UTC
I'm almost certain that you have two copies of poi in your classpath, and that's what is breaking things. I'd suggest you try the code in the faq for checking where classes come from, and that ought to help you spot your two conflicting versions

(It all works fine for me, and for all the unit tests)