[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
This commit is contained in:
PJ Fanning 2020-07-14 16:17:52 +00:00
parent 2f2d6c606a
commit 709a3eb32c
2 changed files with 9 additions and 11 deletions

View File

@ -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;
}

View File

@ -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());
}
}
}