WritableFont doesn’t like to be static!


While using the tool JExcelAPI within my Java application to generate Excel spreadsheets, I got the following exception:

java.lang.ArrayIndexOutOfBoundsException: 5
	at jxl.biff.IndexMapping.getNewIndex(IndexMapping.java:68)
	at jxl.biff.FormattingRecords.rationalize(FormattingRecords.java:388)
	at jxl.write.biff.WritableWorkbookImpl.rationalize(WritableWorkbookImpl.java:988)
	at jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:692)
	...

It appears that this exception occurs only when you try to generate more than one Excel spreadsheet! How strange is that! 😯
After a bit of investigation, it seems that the problem comes from the use of the static modifier with a jxl.write.WritableFont variable.

I unfortunately don’t have the time to check the JExcelAPI code source to understand the root cause.
So my advice would be: “if you get EXACTLY the same stack trace, simply delete any static modifier you used with the WritableFont variables”. 🙂

, , , , ,


  1. #1 by GM Raj on 25 Jan 2011 - 14:35

    Hi,

    Thanks for this posting. Exactly after an year of this posting, I got benefited. This really helped me to overcome my one full day headache.

    Thanks again.

  2. #2 by remi on 01 Jul 2011 - 15:57

    THX a lot ! It works perfectly, just by removing static word (final is still OK).

  3. #3 by Mr. Me on 11 Apr 2013 - 16:27

    Thanks a lot! Works for me:-)

  4. #4 by wasttguy on 30 May 2013 - 15:28

    Thanks for this post, it looks like this problem is still an issue.
    As remi suggested, the problem seems to be the static modifier.

  5. #5 by Arjun Chandran on 11 Jun 2014 - 14:24

    Awesome suggestion.Helped me with an irritating issue.
    I had not made this WritableFont static but it was a global variable in my class.But it seems that for everynew font being used WritableFont need to newly instantiated.
    Anyways your suggestion helped me.

  6. #6 by Gaddy on 01 Jul 2015 - 13:21

    From
    http://jexcelapi.sourceforge.net/resources/faq/

    “also, it’s important that you Do Not declare your cell formats as static. As a cell format is added to a sheet, it gets assigned an internal index number. If you have two threads (such as in a Web environment) writing two different spreadsheets, the format numbers will get confused and the resulting sheet could either be corrupted or incorrect.”

  7. #7 by robin on 03 Sep 2015 - 13:27

    I thank you too. It works.
    Even four years after, this post continue to help

  8. #8 by Tom on 09 Dec 2015 - 10:41

    Saved a day for me!
    Had an hour to prepare Excel file export and got stuck on this exception.
    Everything works fine after having removed static modifier for NumberFormat.

(will not be published)