This is an automated email from the ASF dual-hosted git repository.
fanningpj pushed a commit to branch 5.5.x
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/5.5.x by this push:
new 9ad8e753a6 Fix NPE in XWPFParagraph.getCTSpacing(). (#950)
9ad8e753a6 is described below
commit 9ad8e753a6aef786d4365350356bd183ae36080f
Author: Jacobo Aragunde Pérez <[email protected]>
AuthorDate: Mon Nov 24 20:44:02 2025 +0100
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.
---
.../apache/poi/xwpf/usermodel/XWPFParagraph.java | 3 ++
.../poi/xwpf/usermodel/TestXWPFParagraph.java | 44 +++++++++++++++++++++
test-data/document/emptyPPr.docx | Bin 0 -> 13349 bytes
3 files changed, 47 insertions(+)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
index d572da627e..89a681ecf8 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java
@@ -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();
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java
b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java
index 2aaf6b1072..5ef646a77c 100644
---
a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java
+++
b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFParagraph.java
@@ -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());
diff --git a/test-data/document/emptyPPr.docx b/test-data/document/emptyPPr.docx
new file mode 100644
index 0000000000..7ef696b1e1
Binary files /dev/null and b/test-data/document/emptyPPr.docx differ
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]