Bug 60373 - TableCell.getTextHeight() returns Null pointer Exception

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1770447 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2016-11-18 23:29:53 +00:00
parent 5ec38746a1
commit ea63780926
4 changed files with 50 additions and 8 deletions

View File

@ -137,8 +137,10 @@ public class DrawTableShape extends DrawShape {
for (int row=0; row<rows; row++) {
for (int col=0; col<cols; col++) {
TableCell<?,?> tc = ts.getCell(row, col);
DrawTextShape dts = df.getDrawable(tc);
dts.drawContent(graphics);
if (tc != null) {
DrawTextShape dts = df.getDrawable(tc);
dts.drawContent(graphics);
}
}
}
}
@ -229,6 +231,9 @@ public class DrawTableShape extends DrawShape {
* @param args the border attributes
*/
private static void setEdges(TableCell<?,?> cell, BorderEdge edges[], Object... args) {
if (cell == null) {
return;
}
for (BorderEdge be : edges) {
if (be != null) {
if (args.length == 0) {

View File

@ -297,7 +297,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
double maxHeight = 0;
for (int col=0; col<cols; col++) {
XSLFTableCell tc = getCell(row, col);
if (tc.getGridSpan() != 1 || tc.getRowSpan() != 1) {
if (tc == null || tc.getGridSpan() != 1 || tc.getRowSpan() != 1) {
continue;
}
// need to set the anchor before height calculation
@ -314,8 +314,10 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
for (int col=0; col<cols; col++) {
Rectangle2D bounds = new Rectangle2D.Double(newX, newY, colWidths[col], rowHeights[row]);
XSLFTableCell tc = getCell(row, col);
tc.setAnchor(bounds);
newX += colWidths[col]+DrawTableShape.borderSize;
if (tc != null) {
tc.setAnchor(bounds);
newX += colWidths[col]+DrawTableShape.borderSize;
}
}
newY += rowHeights[row]+DrawTableShape.borderSize;
}
@ -324,6 +326,9 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
for (int row=0; row<rows; row++) {
for (int col=0; col<cols; col++) {
XSLFTableCell tc = getCell(row, col);
if (tc == null) {
continue;
}
Rectangle2D mergedBounds = tc.getAnchor();
for (int col2=col+1; col2<col+tc.getGridSpan(); col2++) {
assert(col2 < cols);

View File

@ -112,8 +112,10 @@ public class TestTable {
int col = 0;
for (TextDirection td : tds) {
TableCell<?,?> c = tbl1.getCell(0, col++);
c.setTextDirection(td);
c.setText("bla");
if (c != null) {
c.setTextDirection(td);
c.setText("bla");
}
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();

View 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;
}
}