From 6d6089eee5756f472f330a4d2860cf46d921a23c Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 5 Dec 2025 10:51:57 +0100 Subject: [PATCH] nullSafeFormatProperties (#961) --- .../main/java/org/apache/poi/ss/util/CellUtil.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java index b8de646192..faa1722b73 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java +++ b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java @@ -614,7 +614,7 @@ public final class CellUtil { CellStyle originalStyle = cell.getCellStyle(); CellStyle newStyle = null; - EnumMap values = originalStyle.getFormatProperties(); + EnumMap values = nullSafeFormatProperties(originalStyle); if (properties.containsKey(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR) && properties.get(CellPropertyType.FILL_FOREGROUND_COLOR_COLOR) == null) { values.remove(CellPropertyType.FILL_FOREGROUND_COLOR); } @@ -635,7 +635,7 @@ public final class CellUtil { for (int i = 0; i < numberCellStyles; i++) { CellStyle wbStyle = workbook.getCellStyleAt(i); - EnumMap wbStyleMap = wbStyle.getFormatProperties(); + EnumMap wbStyleMap = nullSafeFormatProperties(wbStyle); // the desired style already exists in the workbook. Use the existing style. if (styleMapsMatch(wbStyleMap, values, disableNullColorCheck)) { @@ -653,6 +653,13 @@ public final class CellUtil { cell.setCellStyle(newStyle); } + // try to get format properties from CellStyle but the method may return null + // in some implementations, so we need to be null safe here + private static EnumMap nullSafeFormatProperties(CellStyle style) { + EnumMap props = style.getFormatProperties(); + return props == null ? getFormatProperties(style) : props; + } + private static boolean styleMapsMatch(final Map newProps, final Map storedProps, final boolean disableNullColorCheck) { final EnumMap map1Copy = new EnumMap<>(newProps); @@ -888,7 +895,7 @@ public final class CellUtil { if (src == null || dest == null) { throw new IllegalArgumentException("Source and destination styles must not be null"); } - EnumMap properties = src.getFormatProperties(); + EnumMap properties = nullSafeFormatProperties(src); setFormatProperties(dest, destWorkbook, properties); }