Hello there!
I use several tables in my created pdf document to set the text layout.
At some step I use up to three tables (with background), if the next table would cause a page break a NullPointerException occurs.
This NPE occurs at
Caused by: com.lowagie.text.DocumentException: java.lang.NullPointerException
at com.lowagie.text.pdf.PdfDocument.add(PdfDocument.java:770)
at com.lowagie.text.Document.add(Document.java:293)
at de.msg.vorsorge.all.pdf.PdfCTabelle.go(PdfCTabelle.java:337)
... 10 more
Caused by: java.lang.NullPointerException
at com.lowagie.text.pdf.PdfContentByte.setGState(PdfContentByte.java:3042)
at com.lowagie.text.pdf.PdfContentByte.saveColorFill(PdfContentByte.java:2350)
at com.lowagie.text.pdf.PdfContentByte.setRGBColorFill(PdfContentByte.java:2233)
at com.lowagie.text.pdf.PdfContentByte.setColorFill(PdfContentByte.java:2343)
at com.lowagie.text.pdf.PdfContentByte.rectangle(PdfContentByte.java:1029)
at com.lowagie.text.pdf.PdfDocument.renderCells(PdfDocument.java:3037)
at com.lowagie.text.pdf.PdfDocument.addPdfTable(PdfDocument.java:2694)
at com.lowagie.text.pdf.PdfDocument.add(PdfDocument.java:729)
... 12 more
In this context a PdfContentByte(null) is created in PdfDocument.addPdfTable.
Then, in PdfContentByte.setGState, the (not initalized) writer is used ... NPE occurs.
It occurs as I see now, when a page break occurs and the table uses a background color (lightgrey).
I use .setTableFitsPage(true), but when using .setTableFitsPage(false), there is the NPE as well.
As long as the the table with background color does not exceed the remaining space (or no background color ist used), it is drawn, too.
So the NPE occurs when exceeding the remaining page size and when background color is used.
NOTE: with openPdf 1.3.11 my code worked and no NPE occured. Is it a bug since 1.3.12 or a bug or too-old-fashioned code in my project?
OK, I have a test class for this error, maybe this helps ...
If the cell exceeds the remaining space AND a background is used, the NEP occurs.
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.PdfWriter;
public class TestClass {
public static void main(String[] args) {
try {
final String loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." + "\r\n" + "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.\r\n" +
"\r\n" +
"Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.\r\n" +
"\r\n" +
"Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. ";
final Cell cLoremIpsum = new Cell(new Paragraph(loremIpsum));
cLoremIpsum.setBackgroundColor(Color.LIGHT_GRAY);
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("fail.pdf"));
Table table = new Table(1, 2);
table.setWidth(100);
// table.setCellsFitPage(true);
table.addCell(cLoremIpsum);
table.addCell(cLoremIpsum);
document.open();
document.add(new Paragraph("Hello World"));
document.add(table);
document.close();
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
as I said, in openPDF 1.3.11 this works, with 1.3.12 up to 1.3.17, the NPE occurs.
table.setConvert2pdfptable(true);
prevents the NPE, but I cannot use this approach because of rowspan > 1, which does not work with PdfPCell.
Thanks for reporting this bug.
This seems to be the relevant code where the NullpointerException occurs:
https://github.com/LibrePDF/OpenPDF/blame/1.3.12/openpdf/src/main/java/com/lowagie/text/pdf/PdfContentByte.java#L3041
This seems to be the relevant pull request:
https://github.com/LibrePDF/OpenPDF/pull/282
Perhaps we can just add some checks to protect this method from NPExceptions?
Pull requests welcome!
I have added a fix to the master branch here: https://github.com/LibrePDF/OpenPDF/commit/2c53d754178c7e20c250d1581b2e7ca010591aa9
@arnthom Can you please try the latest master branch from git of OpenPDF, and see if the problem has been solved for you there?
Yes! @andreasrosdal it works, thank you!
OpenPDF 1.3.18 has been released today, with this fix included.