]> source.dussan.org Git - poi.git/commitdiff
Bug 60373 - TableCell.getTextHeight() returns Null pointer Exception
authorAndreas Beeker <kiwiwings@apache.org>
Fri, 18 Nov 2016 23:29:53 +0000 (23:29 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Fri, 18 Nov 2016 23:29:53 +0000 (23:29 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1770447 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/sl/draw/DrawTableShape.java
src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFTable.java
src/ooxml/testcases/org/apache/poi/sl/TestTable.java
src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java

index 970c9b4e0b67ab73a8fdbd6243eeeb4fd01b3011..947b2ad13ce4ee078f8068008bb71c49bd538b89 100644 (file)
@@ -137,8 +137,10 @@ public class DrawTableShape extends DrawShape {
         for (int row=0; row<rows; row++) {\r
             for (int col=0; col<cols; col++) {\r
                 TableCell<?,?> tc = ts.getCell(row, col);\r
-                DrawTextShape dts = df.getDrawable(tc);\r
-                dts.drawContent(graphics);\r
+                if (tc != null) {\r
+                    DrawTextShape dts = df.getDrawable(tc);\r
+                    dts.drawContent(graphics);\r
+                }\r
             }\r
         }\r
     }\r
@@ -229,6 +231,9 @@ public class DrawTableShape extends DrawShape {
      * @param args the border attributes\r
      */\r
     private static void setEdges(TableCell<?,?> cell, BorderEdge edges[], Object... args) {\r
+        if (cell == null) {\r
+            return;\r
+        }\r
         for (BorderEdge be : edges) {\r
             if (be != null) {\r
                 if (args.length == 0) {\r
index 61ab6b44d7cd1d6a67a84665ed63f53e1bfdaafa..2637381d3e2e34be79dff545a4d44689d3c75799 100644 (file)
@@ -297,7 +297,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
             double maxHeight = 0;\r
             for (int col=0; col<cols; col++) {\r
                 XSLFTableCell tc = getCell(row, col);\r
-                if (tc.getGridSpan() != 1 || tc.getRowSpan() != 1) {\r
+                if (tc == null || tc.getGridSpan() != 1 || tc.getRowSpan() != 1) {\r
                     continue;\r
                 }\r
                 // need to set the anchor before height calculation\r
@@ -314,8 +314,10 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
             for (int col=0; col<cols; col++) {\r
                 Rectangle2D bounds = new Rectangle2D.Double(newX, newY, colWidths[col], rowHeights[row]);\r
                 XSLFTableCell tc = getCell(row, col);\r
-                tc.setAnchor(bounds);\r
-                newX += colWidths[col]+DrawTableShape.borderSize;\r
+                if (tc != null) {\r
+                    tc.setAnchor(bounds);\r
+                    newX += colWidths[col]+DrawTableShape.borderSize;\r
+                }\r
             }\r
             newY += rowHeights[row]+DrawTableShape.borderSize;\r
         }\r
@@ -324,6 +326,9 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
         for (int row=0; row<rows; row++) {\r
             for (int col=0; col<cols; col++) {\r
                 XSLFTableCell tc = getCell(row, col);\r
+                if (tc == null) {\r
+                    continue;\r
+                }\r
                 Rectangle2D mergedBounds = tc.getAnchor();\r
                 for (int col2=col+1; col2<col+tc.getGridSpan(); col2++) {\r
                     assert(col2 < cols);\r
index 1dec172dea1a1a844046e5418b1cd9806e659ee1..ac02a62b46cab967ad41b92db1454cb5f30dbce5 100644 (file)
@@ -112,8 +112,10 @@ public class TestTable {
             int col = 0;\r
             for (TextDirection td : tds) {\r
                 TableCell<?,?> c = tbl1.getCell(0, col++);\r
-                c.setTextDirection(td);\r
-                c.setText("bla");\r
+                if (c != null) {\r
+                    c.setTextDirection(td);\r
+                    c.setText("bla");\r
+                }\r
             }\r
             \r
             ByteArrayOutputStream bos = new ByteArrayOutputStream();\r
index 7c02ba282ba5af90e1405e99908397cdcb10615e..759bbf6fb528296121eb36c888665d9515bd8022 100644 (file)
@@ -60,6 +60,10 @@ import org.apache.poi.xslf.usermodel.XSLFShape;
 import org.apache.poi.xslf.usermodel.XSLFSlide;
 import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
 import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
+import org.apache.poi.xslf.usermodel.XSLFTable;
+import org.apache.poi.xslf.usermodel.XSLFTableCell;
+import org.apache.poi.xslf.usermodel.XSLFTableRow;
+import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
 import org.apache.poi.xslf.usermodel.XSLFTextRun;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -556,4 +560,30 @@ public class TestXSLFBugs {
         rwPptx.close();
         ppt.close();
     }
-}
+
+
+    @Test
+    public void bug60373() throws IOException {
+        XMLSlideShow ppt = new XMLSlideShow();
+        XSLFSlide sl = ppt.createSlide();
+        XSLFTable t = sl.createTable();
+        XSLFTableRow r = t.addRow();
+        bug60373_addCell(r);
+        bug60373_addCell(r);
+        r = t.addRow();
+        XSLFTableCell c = bug60373_addCell(r);
+        // call getTextHeight, when table is not fully populated
+        double th = c.getTextHeight();
+        assertTrue(th > 10);
+        ppt.close();
+    }
+
+    private static XSLFTableCell bug60373_addCell(XSLFTableRow r) {
+        XSLFTableCell cell = r.addCell();
+        XSLFTextParagraph p = cell.addNewTextParagraph();
+        XSLFTextRun tr = p.addNewTextRun();
+        tr.setText("t");
+        return cell;
+    }
+
+}
\ No newline at end of file