Fix ArrayIndexOutOfBoundsException if XWPFRun does not set style

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1889259 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sayi 2021-04-28 09:26:16 +00:00
parent 285fd61fb8
commit 90f228cabb
2 changed files with 13 additions and 7 deletions

View File

@ -1248,16 +1248,11 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
*/
public String getStyle() {
CTRPr pr = getCTR().getRPr();
if (pr == null) {
if (pr == null || pr.sizeOfRStyleArray() <= 0) {
return "";
}
CTString style = pr.getRStyleArray(0);
if (style == null) {
return "";
}
return style.getVal();
return null == style ? "" : style.getVal();
}

View File

@ -788,6 +788,17 @@ class TestXWPFRun {
document.close();
}
@Test
void testGetEmptyStyle() throws IOException {
XWPFDocument document = new XWPFDocument();
final XWPFRun run = document.createParagraph().createRun();
assertEquals("", run.getStyle());
run.getCTR().addNewRPr();
assertEquals("", run.getStyle());
document.close();
}
@Test
void testGetDepthWidth() throws IOException, InvalidFormatException {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestDocument.docx")) {