NullPointerException while getting text from Page

I try to read text from specific page of pdf document. Below is my code:
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Page page = document.getPages().get_Item(slideNumber + 1);
TextDevice textDevice = new TextDevice();
TextExtractionOptions textExtOptions = new TextExtractionOptions(TextExtractionOptions.TextFormattingMode.Raw);
textDevice.setExtractionOptions(textExtOptions);
textDevice.process(page, outputStream);
return new String(outputStream.toByteArray(), Charset.forName(“UTF-8”));
} catch (IOException e) {
LOGGER.error(“Failed”, e);
throw new RuntimeException(e);
}
}
But I have NullPointerException. Below is stacktrace:

java.lang.NullPointerException: null
at com.aspose.pdf.internal.l5t.ly.lf(Unknown Source)
at com.aspose.pdf.internal.l5t.ly.ly(Unknown Source)
at com.aspose.pdf.internal.l5t.l0t.lI(Unknown Source)
at com.aspose.pdf.internal.l5t.l0t.lI(Unknown Source)
at com.aspose.pdf.internal.l5t.l0t.le(Unknown Source)
at com.aspose.pdf.internal.l5t.l0t.(Unknown Source)
at com.aspose.pdf.internal.l5t.l0t.(Unknown Source)
at com.aspose.pdf.TextAbsorber.visit(Unknown Source)
at com.aspose.pdf.devices.TextDevice.processInternal(Unknown Source)
at com.aspose.pdf.devices.TextDevice.process(Unknown Source)

Does someone know how I can fix it? Thanks!

@artemsavitsky

Could you please make sure that you are using Aspose.PDF for Java 19.4 and if issue is still occurring, please share your sample PDF document with us. We will test the scenario in our environment and address it accordingly.

@asad.ali Yes, I’m using 19.4 version. test-pdf.pdf (30.6 KB)

@asad.ali
And also I tried get text from page with this code:
Page page = document.getPages().get_Item(slideNumber);
TextAbsorber textAbsorber = new TextAbsorber();
page.accept(textAbsorber);
return textAbsorber.getText();
But had the same issue in calling the last line of code.

@artemsavitsky

Thanks for sharing sample PDF.

We have tested the scenario in our environment while using Aspose.PDF for Java 19.4 and were unable to notice the issue that you have mentioned. For your kind reference, a screenshot of console output and code snippet is also attached.

output.png (37.3 KB)

Would you please share your complete environment details i.e. OS Name and Version, Application Type, etc. It would also be helpful if you can please share a sample console application which is able to reproduce the error. We will again test the scenario in our environment and address it accordingly.

@asad.ali
Here is jar (change extension from zip to jar )
to execute: java -jar demo-0.0.1-SNAPSHOT.jar /home/user/test-pdf.pdf 4

@asad.ali
I executed jar in docker container java:openjdk-8-jdk-alpine

@artemsavitsky

We could not find any JAR with your post. Would you please make sure to upload it to Dropbox or Google Drive and share the link with us. We will further proceed accordingly.