From 709a3eb32c6e92cf4e3a93c7cbcdc0a19738b662 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 14 Jul 2020 16:17:52 +0000 Subject: [PATCH] [bug-64600] Avoid XWPF NPE when styleid is null. Thanks to Sayi. This closes #186 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1879859 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/xwpf/usermodel/XWPFStyles.java | 14 +++----------- .../apache/poi/xwpf/usermodel/TestXWPFStyles.java | 6 ++++++ 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java index 0db76db673..75d3acc96d 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java @@ -154,11 +154,7 @@ public class XWPFStyles extends POIXMLDocumentPart { * @return true if style exist, false if style not exist */ public boolean styleExist(String styleID) { - for (XWPFStyle style : listStyle) { - if (style.getStyleId().equals(styleID)) - return true; - } - return false; + return null != getStyle(styleID); } /** @@ -182,12 +178,8 @@ public class XWPFStyles extends POIXMLDocumentPart { */ public XWPFStyle getStyle(String styleID) { for (XWPFStyle style : listStyle) { - try { - if (style.getStyleId().equals(styleID)) - return style; - } catch (NullPointerException e) { - // Ignore NPE - } + if (null != style.getStyleId() && style.getStyleId().equals(styleID)) + return style; } return null; } diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java index 996f50b4f8..d03a13b958 100644 --- a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java +++ b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFStyles.java @@ -217,6 +217,11 @@ public final class TestXWPFStyles { assertNotNull(styles.getStyle("NoList")); assertNull(styles.getStyle("EmptyCellLayoutStyle")); assertNotNull(styles.getStyle("BalloonText")); + + // Bug 64600: styleExist throws NPE + assertTrue(styles.styleExist("NoList")); + assertFalse(styles.styleExist("EmptyCellLayoutStyle")); + assertTrue(styles.styleExist("BalloonText")); } catch (NullPointerException e) { fail(e.toString()); } @@ -235,4 +240,5 @@ public final class TestXWPFStyles { assertEquals(styleName, style.getName()); } } + } -- 2.39.5