This is an automated email from the ASF dual-hosted git repository.

fanningpj pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new d4fc5cd6c0 avoid creating CTTblPr if not needed
d4fc5cd6c0 is described below

commit d4fc5cd6c08338a4132a6348ee1b1b077f9527b8
Author: PJ Fanning <[email protected]>
AuthorDate: Mon Jul 14 17:05:45 2025 +0100

    avoid creating CTTblPr if not needed
---
 .../org/apache/poi/xwpf/usermodel/XWPFTable.java   | 36 ++++++++++++----------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java 
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
index 7de3ec20b6..bbd43447d3 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTable.java
@@ -284,8 +284,8 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
      * @return width value as an integer
      */
     public int getWidth() {
-        CTTblPr tblPr = getTblPr();
-        return tblPr.isSetTblW() ? 
(int)Units.toDXA(POIXMLUnits.parseLength(tblPr.getTblW().xgetW())) : -1;
+        CTTblPr tblPr = getTblPr(false);
+        return tblPr != null && tblPr.isSetTblW() ? 
(int)Units.toDXA(POIXMLUnits.parseLength(tblPr.getTblW().xgetW())) : -1;
     }
 
     /**
@@ -442,7 +442,7 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
      */
     public String getStyleID() {
         String styleId = null;
-        CTTblPr tblPr = ctTbl.getTblPr();
+        CTTblPr tblPr = getTblPr(false);
         if (tblPr != null) {
             CTString styleStr = tblPr.getTblStyle();
             if (styleStr != null) {
@@ -723,8 +723,8 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
 
     public int getRowBandSize() {
         int size = 0;
-        CTTblPr tblPr = getTblPr();
-        if (tblPr.isSetTblStyleRowBandSize()) {
+        CTTblPr tblPr = getTblPr(false);
+        if (tblPr != null && tblPr.isSetTblStyleRowBandSize()) {
             CTDecimalNumber rowSize = tblPr.getTblStyleRowBandSize();
             size = rowSize.getVal().intValue();
         }
@@ -739,8 +739,8 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
 
     public int getColBandSize() {
         int size = 0;
-        CTTblPr tblPr = getTblPr();
-        if (tblPr.isSetTblStyleColBandSize()) {
+        CTTblPr tblPr = getTblPr(false);
+        if (tblPr != null && tblPr.isSetTblStyleColBandSize()) {
             CTDecimalNumber colSize = tblPr.getTblStyleColBandSize();
             size = colSize.getVal().intValue();
         }
@@ -986,12 +986,14 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
     }
 
     private int getCellMargin(Function<CTTblCellMar,CTTblWidth> margin) {
-        CTTblPr tblPr = getTblPr();
-        CTTblCellMar tcm = tblPr.getTblCellMar();
-        if (tcm != null) {
-            CTTblWidth tw = margin.apply(tcm);
-            if (tw != null) {
-                return (int) Units.toDXA(POIXMLUnits.parseLength(tw.xgetW()));
+        CTTblPr tblPr = getTblPr(false);
+        if (tblPr != null) {
+            CTTblCellMar tcm = tblPr.getTblCellMar();
+            if (tcm != null) {
+                CTTblWidth tw = margin.apply(tcm);
+                if (tw != null) {
+                    return (int) 
Units.toDXA(POIXMLUnits.parseLength(tw.xgetW()));
+                }
             }
         }
         return 0;
@@ -1141,7 +1143,8 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
      * @since 4.0.0
      */
     public double getWidthDecimal() {
-        return getWidthDecimal(getTblPr().getTblW());
+        CTTblPr pr = getTblPr(false);
+        return pr == null ? 0.0 : getWidthDecimal(pr.getTblW());
     }
 
     /**
@@ -1173,11 +1176,12 @@ public class XWPFTable implements IBodyElement, 
ISDTContents {
      * A table width can be specified as an absolute measurement (an integer
      * number of twips), a percentage, or the value "AUTO".
      *
-     * @return The width type.
+     * @return The width type. Returns {@link TableWidthType#NIL} as a default.
      * @since 4.0.0
      */
     public TableWidthType getWidthType() {
-        return getWidthType(getTblPr().getTblW());
+        CTTblPr pr = getTblPr(false);
+        return pr == null ? TableWidthType.NIL : getWidthType(pr.getTblW());
     }
 
     /**


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to