Save to pdf NullPointerException and low performance

simple code

        Document doc = new Document();
        doc.processParagraphs();
        Paragraphs paragraphs = doc.getPages().add().getParagraphs();
        Table table = new Table();
        table.setInNewPage(false);
        table.setAlignment(HorizontalAlignment.Center);
        table.setDefaultCellPadding(cellMarginInfo);
        table.setDefaultCellBorder(defaultCellBorder);
        table.setDefaultCellTextState(state);
        table.setBordersIncluded(true);
        table.setColumnAdjustment(ColumnAdjustment.Customized);
        table.setColumnWidths(String.valueOf(WIDTH));
        table.getRows().add().getCells().add("foo");
        paragraphs.add(table);
//skip  many tables
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        doc.save(outputStream);

I meet two problems:

  • performance is slow when first call table.getRows().add().getCells().add("foo"); ,at least 100ms,sometime is 5s.
  • sometime not always thrown exception:
java.lang.NullPointerException: null
        at com.aspose.pdf.internal.l4k.l1h.l0p(Unknown Source)
        at com.aspose.pdf.internal.l4j.lk.lI(Unknown Source)
        at com.aspose.pdf.internal.l4k.l1h.l1h(Unknown Source)
        at com.aspose.pdf.internal.l4k.l2t.lI(Unknown Source)
        at com.aspose.pdf.internal.l4k.l1h.lI(Unknown Source)
        at com.aspose.pdf.internal.l4k.l1h.lI(Unknown Source)
        at com.aspose.pdf.internal.l5t.ld.lI(Unknown Source)
        at com.aspose.pdf.TextState.setFont(Unknown Source)
        at com.aspose.pdf.TextState.lI(Unknown Source)
        at com.aspose.pdf.TextState.lI(Unknown Source)
        at com.aspose.pdf.TextSegment.lI(Unknown Source)
        at com.aspose.pdf.TextBuilder.lI(Unknown Source)
        at com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source)
        at com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source)
        at com.aspose.pdf.l11p.lI(Unknown Source)
        at com.aspose.pdf.l11p.lI(Unknown Source)
        at com.aspose.pdf.l11p.le(Unknown Source)
        at com.aspose.pdf.Cell.lI(Unknown Source)
        at com.aspose.pdf.Row.lI(Unknown Source)
        at com.aspose.pdf.Table.lI(Unknown Source)
        at com.aspose.pdf.l11p.lI(Unknown Source)
        at com.aspose.pdf.l11p.le(Unknown Source)
        at com.aspose.pdf.Page.lf(Unknown Source)
        at com.aspose.pdf.Page.lk(Unknown Source)
        at com.aspose.pdf.ADocument.processParagraphs(Unknown Source)
        at com.aspose.pdf.Document.processParagraphs(Unknown Source)
        at com.aspose.pdf.ADocument.lf(Unknown Source)
        at com.aspose.pdf.ADocument.saveInternal(Unknown Source)
        at com.aspose.pdf.Document.saveInternal(Unknown Source)
        at com.aspose.pdf.ADocument$3.lI(Unknown Source)
        at com.aspose.pdf.internal.l82l.lf.lf(Unknown Source)
        at com.aspose.pdf.internal.l82l.lj.lI(Unknown Source)
        at com.aspose.pdf.ADocument.save(Unknown Source)
        at com.aspose.pdf.Document.save(Unknown Source)

@yokv

Thank you for contacting support.

The code snippet shared by you includes some undeclared variables like state, Width etc Would you please share SSCCE code so that we may try to reproduce and investigate it in our environment. Also mention your environment details including JDK/JRE version and OS details for our reference.

Before sharing requested information, please ensure using Aspose.PDF for Java 18.10.2.

environment : jdk1.8, win10


import com.aspose.pdf.*;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

public class PdfTest {
    private static Logger logger = LoggerFactory.getLogger(PdfTest.class);

    @Test
    public void testPdf() throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            new Thread(() -> generatePDF()).start();
        }
        Thread.currentThread().join();
    }

    private void generatePDF() {
        long start = System.currentTimeMillis();
        final int WIDTH = 400;
        final float BORDER_WIDTH = .1f;
        final BorderInfo borderInfo = new BorderInfo(BorderSide.All, BORDER_WIDTH);
        Font font = FontRepository.findFont("SimHei");
        MarginInfo cellMarginInfo = new MarginInfo();
        cellMarginInfo.setTop(3f);
        cellMarginInfo.setRight(3f);
        cellMarginInfo.setLeft(3f);
        cellMarginInfo.setBottom(3f);

        TextState textState = new TextState();
        textState.setFontSize(10);
        textState.setHorizontalAlignment(HorizontalAlignment.Left);
        textState.setFont(font);

        Document doc = new Document();
        doc.processParagraphs();
        Paragraphs paragraphs = doc.getPages().add().getParagraphs();
        Table table = new Table();
        table.setInNewPage(false);
        table.setAlignment(HorizontalAlignment.Center);
        table.setDefaultCellPadding(cellMarginInfo);
        table.setDefaultCellBorder(borderInfo);
        table.setDefaultCellTextState(textState);
        table.setBordersIncluded(true);
        table.setColumnAdjustment(ColumnAdjustment.Customized);
        table.setColumnWidths(String.valueOf(WIDTH));
        long mid = System.currentTimeMillis();
        logger.info("table cost  {}", mid - start);
        table.getRows().add().getCells().add("foo");
        logger.info("data cost  {}", System.currentTimeMillis() - mid);
        paragraphs.add(table);
//skip  many tables
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            doc.save(outputStream);
            Resource resource = new InputStreamResource(new ByteArrayInputStream(outputStream.toByteArray()));
            logger.info("cost  {}", System.currentTimeMillis() - start);
        } catch (Exception e) {
            logger.error("error", e);
        }
    }

}

@yokv

Thank you for sharing updated code.

We have not been able to reproduce NullPointerException despite several executions, and you are probably noticing the first call to be comparatively slower because necessary allocation of resources takes place during first iteration occurs. Upon subsequent iterations, the performance gets even better.

We hope this will be helpful. Please ensure using latest version of the API and contact us if you need any further assistance. We will be more than happy to assist you.

thank you anyway