We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Null pointer exception for processParagraph in aspose 20.10

We have just moved to java 11 and aspose 20.10
we are having issue when we generate the pdf for second time

java.lang.NullPointerException: null
at com.aspose.pdf.internal.l5h.l1j.l3y(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.TextState.setFont(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.TextState.lI(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.TextState.lI(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.TextSegment.lI(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.TextBuilder.lI(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.TextBuilder.appendParagraph(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.l11y.lI(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.l11y.lI(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.l11y.le(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.Page.lf(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.Page.lc(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.ADocument.processParagraphs(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]
at com.aspose.pdf.Document.processParagraphs(Unknown Source) ~[aspose.pdf-20.10.jar:20.10]

Below is code

public byte[] generatePdf()
throws Exception {

	//Create a section in the Pdf object and page setup
	boolean isExportToPDF = true;
	pdfDocument = new Document();
	
	ByteArrayOutputStream pdfFile = new ByteArrayOutputStream();

	String absolute= absolutePath(false);
	//for setting fonts and license 	

	String fontPath =  absolute+"fonts/msttcorefonts/";
	FontRepository.addLocalFontPath(fontPath);

	License license= new com.aspose.pdf.License();
	String lic 	= PDFConstants.licence;
	InputStream is = new ByteArrayInputStream(lic.getBytes());
	license.setLicense(is);

	Page page =pdfDocument.getPages().add();

	page.getPageInfo().setHeight(PageSize.getA4().getHeight());
	page.getPageInfo().setWidth(PageSize.getA3().getWidth());
	page.getPageInfo().getMargin().setTop(72);
	page.getPageInfo().getMargin().setBottom(72);
	page.getPageInfo().getMargin().setLeft(72);
	page.getPageInfo().getMargin().setRight(72);

	TextFragment t1 = new TextFragment("This is a test");


t1.getTextState().setFont(arialFont);
	t1.getTextState().setFontSize(12);
	t1.getTextState().setForegroundColor(gray);
	t1.getTextState().setFontStyle(bold);
	t1.getTextState().setHorizontalAlignment(HorizontalAlignment.Center);

	page.getParagraphs().add(t1);
	page.getParagraphs().add(PDFConstants.newLine);
	page.getParagraphs().add(PDFConstants.newLine);

	    
	byte[] arrayOfByte = null;
	//Rendering PDF sections
	try {

		
		TextStamp textStamp = new TextStamp("DRAFT");
		// set whether stamp is background
		textStamp.setBackground(true);
		// rotate stamp
		textStamp.setRotateAngle(45);
		// set origin
		textStamp.setXIndent(100);
		textStamp.setYIndent(100);

		// set text properties
		textStamp.getTextState().setFont(arialFont);
		textStamp.getTextState().setFontSize(200.0F);
		textStamp.getTextState().setFontStyle(bold);
		textStamp.getTextState().setForegroundColor(water);
		// add stamp to all pages			           
		pdfDocument.processParagraphs();					
		for (int pageCounter = 1; pageCounter <= pdfDocument.getPages().size(); pageCounter++) {
			pdfDocument.getPages().get_Item(pageCounter).addStamp(textStamp);
		}			
		pdfDocument.optimize();
		pdfDocument.setPageLayout(PageLayout.OneColumn);
		pdfDocument.save(pdfFile);	
		arrayOfByte = pdfFile.toByteArray();	
		pdfDocument.close();
		pdfFile.close();
		logger.info("arrayOfByte Size" +arrayOfByte.length);
	} catch (Exception  e) {
		logger.error("Exception in EWRGeneratePDF.reviewCompletePdf() is :: ",e);
	} 
	return arrayOfByte ;//pdfFile.toByteArray();		
}

public static void main(String[] args) {
GeneratePDF pdf = new GeneratePDF();
GeneratePDF pdf2 = new GeneratePDF();
try {
byte[] arrayOfByte = pdf.generatePdf( );
System.out.println(“arrayOfByte Size first…” +arrayOfByte.length);
pdf = null;
System.gc();
byte[] arrayOfByte2 = pdf2.generatePdf( );
System.out.println(“arrayOfByte Size second…” +arrayOfByte2.length);
pdf = null;
System.gc();
} catch (Exception e) {
e.printStackTrace();
}
}

Issue comes for second time time excution
Plz let me know asap

@MaddyMaddy

We tested using JDK 11 and Aspose.PDF for Java 21.5 in our environment. We did not notice any exception. Furthermore, it seems like the API is unable to access the font at your end as thrown exception seems related to it. Please make sure that font stream is available in case you are specifying font using stream. In case issue still persists, please share the Linux version and name which you are using at your end. We will further proceed to assist you accordingly.