Bug 66230 - SheetUtil.getDefaultCharWidth() should handle AWT NPE instead of failing
Summary: SheetUtil.getDefaultCharWidth() should handle AWT NPE instead of failing
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: SS Common (show other bugs)
Version: unspecified
Hardware: PC Linux
: P2 major (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks: 65260
  Show dependency tree
 
Reported: 2022-08-18 16:51 UTC by Patric Rufflar
Modified: 2022-12-26 15:48 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Patric Rufflar 2022-08-18 16:51:46 UTC
Apache POI (e.g. SheetUtil.getDefaultCharWidth()) fails with a NPE when using auto-sized columns and some system packages (e.g. fontconfig) are not installed on the system.
Instead of failing, POI should rather catch the NPE and disable column auto-sizing like it is doing in other scenarios/exception types.

Stacktrace (from poi 3.17):

 java.lang.NullPointerException
                at java.desktop/sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1262)
                at java.desktop/sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:225)
                at java.desktop/sun.awt.FontConfiguration.init(FontConfiguration.java:107)
                at java.desktop/sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:719)
                at java.desktop/sun.font.SunFontManager$2.run(SunFontManager.java:367)
                at java.base/java.security.AccessController.doPrivileged(Native Method)
                at java.desktop/sun.font.SunFontManager.<init>(SunFontManager.java:312)
                at java.desktop/sun.awt.FcFontManager.<init>(FcFontManager.java:35)
                at java.desktop/sun.awt.X11FontManager.<init>(X11FontManager.java:56)
                at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
                at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
                at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
                at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
                at java.desktop/sun.font.FontManagerFactory$1.run(FontManagerFactory.java:84)
                at java.base/java.security.AccessController.doPrivileged(Native Method)
                at java.desktop/sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
                at java.desktop/java.awt.Font.getFont2D(Font.java:497)
                at java.desktop/java.awt.Font.canDisplayUpTo(Font.java:2246)
                at java.desktop/java.awt.font.TextLayout.singleFont(TextLayout.java:469)
                at java.desktop/java.awt.font.TextLayout.<init>(TextLayout.java:530)
                at org.apache.poi.ss.util.SheetUtil.getDefaultCharWidth(SheetUtil.java:275)
                at org.apache.poi.xssf.streaming.AutoSizeColumnTracker.<init>(AutoSizeColumnTracker.java:117)
                at org.apache.poi.xssf.streaming.SXSSFSheet.<init>(SXSSFSheet.java:82)
                at org.apache.poi.xssf.streaming.SXSSFWorkbook.createAndRegisterSXSSFSheet(SXSSFWorkbook.java:658)
                at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:679)
Comment 1 PJ Fanning 2022-08-18 17:00:20 UTC
Can you just disable auto-sizing of columns in your SXSSFSheet(s)?

I would be reluctant to silently ignore issues with sizing columns. It is an optional feature and if your environment doesn't allow it, it is easy to disable it.
Comment 2 PJ Fanning 2022-08-18 17:01:38 UTC
PS We don't support POI 3.17. If we made changes, it would be in POI 5.2.x.
Comment 3 PJ Fanning 2022-08-18 17:11:27 UTC
Actually, I see that the tracker for autosizing is initialised even if you don't explicitly try to autosize columns. So this looks like an ok change to make - but still it will be aimed at release 5.2.3 and will not be backported.
Comment 4 PJ Fanning 2022-08-18 17:40:45 UTC
I added r1903529
Comment 5 Patric Rufflar 2022-08-18 17:48:08 UTC
Thanks for your quick response and fix!
First of all a fix in 5.2.x would be fine for me - thanks!

1. I agree - at least avoiding the NPE when not using auto-size at all is the minimum we should do.


2. However, regarding "It is an optional feature and if your environment doesn't allow it, it is easy to disable it.".

My problem is: 
We are not in control of each environment in which our products are used (e.g. at the customer).

Question:
After fixing the issue in #1, I understand that 
org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet() will no longer fail.
So will sheet.autoSizeColumn(colNo) now just throw AWT's NPE in such an environment?
If yes, would it be safe to just catch NPE in our code and continue or is the state of POI (and/or the Worksheet) somehow broken?
Comment 6 PJ Fanning 2022-08-18 18:23:51 UTC
autoSizeColumn method will fail if the auto size tracker has not initialized - an IllegalStateException - it's up to you how to handle this
Comment 7 Patric Rufflar 2022-08-18 18:35:32 UTC
Ok, so it's safe to continue with the worksheet on which setAutoSizeColumn() failed.

Thanks for your effort!