This is an automated email from the ASF dual-hosted git repository.

delei pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fesod.git


The following commit(s) were added to refs/heads/main by this push:
     new 29483d39 fix: prevent NPE in afterWorkbookDispose (#789)
29483d39 is described below

commit 29483d39bf64b6e6d7cce6cdbaf9c86bf92b942e
Author: chani <[email protected]>
AuthorDate: Thu Jan 29 01:51:41 2026 +0900

    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 <[email protected]>
---
 .../impl/DimensionWorkbookWriteHandler.java        |  3 ++
 .../impl/DimensionWorkbookWriteHandlerTest.java    | 50 ++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git 
a/fesod-sheet/src/main/java/org/apache/fesod/sheet/write/handler/impl/DimensionWorkbookWriteHandler.java
 
b/fesod-sheet/src/main/java/org/apache/fesod/sheet/write/handler/impl/DimensionWorkbookWriteHandler.java
index e6575e75..d2b5433b 100644
--- 
a/fesod-sheet/src/main/java/org/apache/fesod/sheet/write/handler/impl/DimensionWorkbookWriteHandler.java
+++ 
b/fesod-sheet/src/main/java/org/apache/fesod/sheet/write/handler/impl/DimensionWorkbookWriteHandler.java
@@ -57,6 +57,9 @@ public class DimensionWorkbookWriteHandler implements 
WorkbookWriteHandler {
             return;
         }
         for (WriteSheetHolder writeSheetHolder : writeSheetHolderMap.values()) 
{
+            if (writeSheetHolder == null) {
+                continue;
+            }
             if (writeSheetHolder.getSheet() == null || 
!(writeSheetHolder.getSheet() instanceof SXSSFSheet)) {
                 continue;
             }
diff --git 
a/fesod-sheet/src/test/java/org/apache/fesod/sheet/write/handler/impl/DimensionWorkbookWriteHandlerTest.java
 
b/fesod-sheet/src/test/java/org/apache/fesod/sheet/write/handler/impl/DimensionWorkbookWriteHandlerTest.java
new file mode 100644
index 00000000..126b1704
--- /dev/null
+++ 
b/fesod-sheet/src/test/java/org/apache/fesod/sheet/write/handler/impl/DimensionWorkbookWriteHandlerTest.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.fesod.sheet.write.handler.impl;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.fesod.sheet.write.metadata.holder.WriteSheetHolder;
+import org.apache.fesod.sheet.write.metadata.holder.WriteWorkbookHolder;
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
+import org.junit.jupiter.api.Test;
+
+class DimensionWorkbookWriteHandlerTest {
+
+    @Test
+    void afterWorkbookDispose_shouldNotThrowNPE_whenMapContainsNullValue() {
+        // Given
+        DimensionWorkbookWriteHandler handler = new 
DimensionWorkbookWriteHandler();
+        WriteWorkbookHolder writeWorkbookHolder = 
mock(WriteWorkbookHolder.class);
+        SXSSFWorkbook workbook = mock(SXSSFWorkbook.class);
+
+        Map<Integer, WriteSheetHolder> sheetHolderMap = new HashMap<>();
+        sheetHolderMap.put(0, null); // null entry that caused NPE
+
+        when(writeWorkbookHolder.getWorkbook()).thenReturn(workbook);
+        
when(writeWorkbookHolder.getHasBeenInitializedSheetIndexMap()).thenReturn(sheetHolderMap);
+
+        // When & Then
+        assertDoesNotThrow(() -> 
handler.afterWorkbookDispose(writeWorkbookHolder));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to