diff options
author | PJ Fanning <fanningpj@apache.org> | 2020-07-14 16:17:52 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2020-07-14 16:17:52 +0000 |
commit | 709a3eb32c6e92cf4e3a93c7cbcdc0a19738b662 (patch) | |
tree | 0c95869b9693f9c1aa2ff3342ffa7c149160615b /src | |
parent | 2f2d6c606acbb7f8fa2f864fa3229a37d7aca3fd (diff) | |
download | poi-709a3eb32c6e92cf4e3a93c7cbcdc0a19738b662.tar.gz poi-709a3eb32c6e92cf4e3a93c7cbcdc0a19738b662.zip |
[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
Diffstat (limited to 'src')
-rw-r--r-- | src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFStyles.java | 14 | ||||
-rw-r--r-- | src/ooxml/testcases/org/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()); } } + } |