When calling a WorkbookFactory.create(InputStream inp) method from multiple threads sometimes following error appears: Caused by: java.lang.NullPointerException at org.apache.poi.ss.usermodel.WorkbookFactory.create (WorkbookFactory.java:167) at org.apache.poi.ss.usermodel.WorkbookFactory.create (WorkbookFactory.java:112) at org.apache.poi.ss.usermodel.WorkbookFactory.create (WorkbookFactory.java:253) at org.apache.poi.ss.usermodel.WorkbookFactory.create (WorkbookFactory.java:221) the reason for this is that in HSSFWorkbookFactory class WorkbookFactory.createHssfByNode field is null in this block: try { initHssf(); return createHssfByNode.apply(root); } first thread starts initialization: private static void initHssf() throws IOException { if (createHssfFromScratch == null) { // HSSF is part of the main jar, so this shouldn't fail ... initFactory("org.apache.poi.hssf.usermodel.HSSFWorkbookFactory", "poi-*.jar"); } } And sometimes the second thread does not go into this condition and thinks that 123 is already initialized, which is why this error comes in.
*And sometimes the second thread does not go into this condition and thinks that createHssfByNode is already initialized, which is why this error comes in. Sorry, for some reason I can’t edit the description.
as a workaround, you should be able to use write a wrapper that has synchronization you could also try using `new HSSFWorkbook(stream)` ideally, the code should support concurrent use but any fix we do could take months to appear in a release
I added this test https://github.com/apache/poi/commit/24345edd5e7a98089abac93417522cced20025f0 but it passes.
The test is very unlikely to trigger it as it only can happen for the first two executions of the future, after that the variable is initialized, Maybe we can somehow "uninitialize" it again, otherwise it will fail very very rarely.
I added some synchronization in https://svn.apache.org/viewvc?view=revision&revision=1877776
Thanks for the quick answers and fix! Do you think this fix can be included in 4.1.3 release?
If no bugs found in it, it will be in the next release but there is no date yet for that release.