PDFBox Pagebreak strange Nullpointer exception

Posted by schneiti on Stack Overflow See other posts from Stack Overflow or by schneiti
Published on 2014-08-21T10:17:14Z Indexed on 2014/08/21 10:20 UTC
Read the original article Hit count: 156

Filed under:
|

I currently try printing out text on multiple pages. For this, I count the number of rows and when they reach a fixed amount a method called pagebreakis executed. After the first pagebreak, when I try setting a font using contentstream.setFont(PDType1Font.HELVETICA, 12); it yields the following errormessage occuring at the described setFont-row.

java.lang.NullPointerException  at org.apache.pdfbox.pdmodel.edit.PDPageContentStream.setFont(PDPageContentStream.java:321)
    at com.xy.deu.xy.abc.db.schemavergleich.PDFDocumenter.drawBGCS(PDFDocumenter.java:781)
    at com.xy.deu.xy.abc.db.schemavergleich.PDFDocumenter.createPDFDocumentation(PDFDocumenter.java:205)
    at com.xy.deu.xy.abc.db.schemavergleich.MainClass.createPDFandOutput(MainClass.java:361)
    at com.xy.deu.xy.abc.db.schemavergleich.MainClass.start(MainClass.java:231)
    at com.xy.deu.xy.abc.db.schemavergleich.MainClass.main(MainClass.java:180)

Below is the code that gets executed as the error occurs.

...
// If Table gets to long for a page -> pagebreak: 
if(currentLines > 37) {
    pageBreak(currentLinePos);      //  TODO Currently causing app to crash
}
...

private void pageBreak(int currentLine) throws Exception {
        contentStream.endText();
        contentStream.close();

        // Create new page
        page = new PDPage(PDPage.PAGE_SIZE_A4);
        doc.addPage( page );

        // Create a new font object selecting one of the PDF base fonts
        font = PDType1Font.HELVETICA;

        // Start a new content stream which will "hold" the to be created content   
            contentStream = new PDPageContentStream(doc, page);

        currentLines = 0;
        mediabox = page.findMediaBox();
        contentStream.beginText();
        contentStream.moveTextPositionByAmount(startX, startY); 
        contentStream.setFont(PDType1Font.HELVETICA, 12); 

    }

Now comes the strange thing: Debugging yields into nothing that is not set. I'll attach a screenshot for you right at the position where the error occurs:

Debugging at setFont(PDType1Font.HELVETICA,12);

© Stack Overflow or respective owner

Related posts about java

Related posts about pdfbox