我不确定该错误消息实际上指向的是什么。需要明确的是,我知道NullPointerException是什么,但我无法弄清楚不应该存在的null。有人知道由null导致此错误的原因吗?

谢谢。

com.lowagie.text.DocumentException: java.lang.NullPointerException
    at com.lowagie.text.pdf.PdfDocument.add(Unknown Source)
    at com.lowagie.text.Document.add(Unknown Source)
...
Caused by: java.lang.NullPointerException
    at com.lowagie.text.pdf.PdfPTable.getRowHeight(Unknown Source)
    at com.lowagie.text.pdf.PdfPTable.calculateHeights(Unknown Source)
    at com.lowagie.text.pdf.PdfPTable.setTotalWidth(Unknown Source)
    at com.lowagie.text.pdf.ColumnText.goComposite(Unknown Source)
    at com.lowagie.text.pdf.ColumnText.go(Unknown Source)
    at com.lowagie.text.pdf.PdfPCell.getMaxHeight(Unknown Source)
    at com.lowagie.text.pdf.PdfPRow.calculateHeights(Unknown Source)
    at com.lowagie.text.pdf.PdfPRow.getMaxHeights(Unknown Source)
    at com.lowagie.text.pdf.PdfPTable.getRowHeight(Unknown Source)
    at com.lowagie.text.pdf.PdfPTable.calculateHeights(Unknown Source)
    at com.lowagie.text.pdf.PdfPTable.setTotalWidth(Unknown Source)
    at com.lowagie.text.pdf.ColumnText.goComposite(Unknown Source)
    at com.lowagie.text.pdf.ColumnText.go(Unknown Source)
    at com.lowagie.text.pdf.ColumnText.go(Unknown Source)
    at com.lowagie.text.pdf.PdfDocument.addPTable(Unknown Source)
    ... 97 more


有问题的代码:

    this.getDocument().open();
    Element body = this.getBody();
    System.out.println(body);
    System.out.println(this.getDocument().toString());
    this.getDocument().add(body);//it errors out here!
    this.getDocument().close();


调试语句返回:

com.lowagie.text.pdf.PdfPTable@1fa0c8f
com.lowagie.text.Document@14eb497

最佳答案

我得到了完全相同的错误和堆栈跟踪,并通过切换到OpenPDF解决了该问题。

OpenPDF基于iText 4,并具有一些其他的错误修复和NPE修复。
OpenPDF正在积极维护中。

https://github.com/LibrePDF/OpenPDF

09-07 14:26