Skip to content

Commit 29483d3

Browse files
csbiydelei
andauthored
fix: prevent NPE in afterWorkbookDispose (#789)
* fix: add null check for WriteSheetHolder in afterWorkbookDispose Add null check for writeSheetHolder before accessing its methods to prevent NullPointerException when the map contains null values. Fixes #788 * fix: prevent NPE in afterWorkbookDispose Map can contain null writeSheetHolder values, causing NPE when accessing getSheet(). Added guard clause for null entries. Fixes #788 * test : add unit test for WriteSheetHolder NPE --------- Co-authored-by: DeleiGuo <delei@apache.org>
1 parent 5f30766 commit 29483d3

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

fesod-sheet/src/main/java/org/apache/fesod/sheet/write/handler/impl/DimensionWorkbookWriteHandler.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public void afterWorkbookDispose(WriteWorkbookHolder writeWorkbookHolder) {
5757
return;
5858
}
5959
for (WriteSheetHolder writeSheetHolder : writeSheetHolderMap.values()) {
60+
if (writeSheetHolder == null) {
61+
continue;
62+
}
6063
if (writeSheetHolder.getSheet() == null || !(writeSheetHolder.getSheet() instanceof SXSSFSheet)) {
6164
continue;
6265
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.fesod.sheet.write.handler.impl;
21+
22+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.when;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
import org.apache.fesod.sheet.write.metadata.holder.WriteSheetHolder;
28+
import org.apache.fesod.sheet.write.metadata.holder.WriteWorkbookHolder;
29+
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
30+
import org.junit.jupiter.api.Test;
31+
32+
class DimensionWorkbookWriteHandlerTest {
33+
34+
@Test
35+
void afterWorkbookDispose_shouldNotThrowNPE_whenMapContainsNullValue() {
36+
// Given
37+
DimensionWorkbookWriteHandler handler = new DimensionWorkbookWriteHandler();
38+
WriteWorkbookHolder writeWorkbookHolder = mock(WriteWorkbookHolder.class);
39+
SXSSFWorkbook workbook = mock(SXSSFWorkbook.class);
40+
41+
Map<Integer, WriteSheetHolder> sheetHolderMap = new HashMap<>();
42+
sheetHolderMap.put(0, null); // null entry that caused NPE
43+
44+
when(writeWorkbookHolder.getWorkbook()).thenReturn(workbook);
45+
when(writeWorkbookHolder.getHasBeenInitializedSheetIndexMap()).thenReturn(sheetHolderMap);
46+
47+
// When & Then
48+
assertDoesNotThrow(() -> handler.afterWorkbookDispose(writeWorkbookHolder));
49+
}
50+
}

0 commit comments

Comments
 (0)