Fix NPE in XWPFParagraph.getCTSpacing(). (#950)

We recently changed the getters so they don't create the PPr object if
it doesn't exist, but we missed adding one null check for the new
situation.

Add a test to exercise all the getters in a case where there isn't a PPr
object.
This commit is contained in:
Jacobo Aragunde Pérez 2025-11-24 20:44:02 +01:00 committed by PJ Fanning
parent 456550b6c5
commit 9ad8e753a6
3 changed files with 47 additions and 0 deletions

View File

@ -1457,6 +1457,9 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents, Para
*/
private CTSpacing getCTSpacing(boolean create) {
CTPPr pr = getCTPPr(create);
if (pr == null) {
return null;
}
CTSpacing ct = pr.getSpacing();
if (create && ct == null) {
ct = pr.addNewSpacing();

View File

@ -921,6 +921,50 @@ public final class TestXWPFParagraph {
}
}
@Test
void testGettersWithEmptyParagraphProperties() throws IOException {
try (XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("emptyPPr.docx")) {
XWPFParagraph p = doc.getParagraphArray(0);
assertNull(p.getNumID());
assertNull(p.getNumIlvl());
assertNull(p.getNumFmt());
assertNull(p.getNumLevelText());
assertNull(p.getNumStartOverride());
assertFalse(p.isKeepNext());
assertFalse(p.isAlignmentSet());
assertEquals(ParagraphAlignment.LEFT, p.getAlignment());
assertEquals(TextAlignment.AUTO, p.getVerticalAlignment());
assertEquals(Borders.NONE, p.getBorderTop());
assertEquals(Borders.NONE, p.getBorderBottom());
assertEquals(Borders.NONE, p.getBorderLeft());
assertEquals(Borders.NONE, p.getBorderRight());
assertEquals(Borders.NONE, p.getBorderBetween());
assertEquals(-1, p.getSpacingAfter());
assertEquals(-1, p.getSpacingAfterLines());
assertEquals(-1, p.getSpacingBefore());
assertEquals(-1, p.getSpacingBeforeLines());
assertEquals(-1, p.getSpacingBetween());
assertEquals(LineSpacingRule.AUTO, p.getSpacingLineRule());
assertEquals(-1, p.getIndentationLeft());
assertEquals(-1, p.getIndentationLeftChars());
assertEquals(-1, p.getIndentationRight());
assertEquals(-1, p.getIndentationRightChars());
assertEquals(-1, p.getIndentationHanging());
assertEquals(-1, p.getIndentationFirstLine());
assertFalse(p.isPageBreak());
assertFalse(p.isWordWrapped());
assertNull(p.getStyleID());
assertNull(p.getStyle());
}
}
private static void checkSearchText(XWPFParagraph paragraph, String search, int beginRun, int endRun, int beginText, int endText,
int beginChar, int endChar) {
TextSegment result = paragraph.searchText(search, new PositionInParagraph());

Binary file not shown.