You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Mark Gibson <ma...@staff.bluematrix.com> on 2025/11/27 20:28:00 UTC

Extending PDFTextStripper - Worked in v2, NullRefException in v3

Hi

We have some old code that extended PDFTextStripper using PDFBox v2.  Recently tried to upgrade to PDFBox v3 (tried 3.03 and 3.06).  We get a Null exception thrown from SetFontAndSize operator.  It looks like "resources" is null.

PDFont font = context.getResources().getFont(fontName);


Tracing through the code in V2, I see that PDFStreamEngine.processOperator() used to call processor.setContext(this).  That sets the context.resources to a non null value.

In V3, there is no call to processor.SetContext(this).

Can anyone the context of why this was removed, and the expectations for user code to resolve the null reference?

Many thanks

Mark

RE: Extending PDFTextStripper - Worked in v2, NullRefException in v3

Posted by Mark Gibson <ma...@staff.bluematrix.com>.
It was failing in our unit tests, where someone had wrapped our derived class instance in a Mockito spy().  With the spy, it breaks.  Without the spy, it works fine.  Not going to pretend I understand why, but I'll take that as a win and change our tests.

Totally unexpected, but sorry for the noise anyway.

Mark
-----Original Message-----
From: Mark Gibson <ma...@staff.bluematrix.com> 
Sent: 27 November 2025 20:47
To: users@pdfbox.apache.org
Cc: users@pdfbox.apache.org
Subject: Re: Extending PDFTextStripper - Worked in v2, NullRefException in v3 

I’ll see if I can test later tonight.  Thanks for the quick response Tilman.

> On 27 Nov 2025, at 20:41, Tilman Hausherr <TH...@t-online.de> wrote:
> 
> [EXTERNAL]
> 
> In 3.0 the context is set in the constructor. But your problem is a 
> different one, that "resources" is null.
> It would be interesting to find out whether this happens in ordinary 
> text extraction. If not, please share minimal code that reproduces 
> this, and the PDF where it happens (or does it happen with any PDF?) 
> Tilman
> 
>> Am 27.11.2025 um 21:28 schrieb Mark Gibson:
>> Hi
>> 
>> We have some old code that extended PDFTextStripper using PDFBox v2.  Recently tried to upgrade to PDFBox v3 (tried 3.03 and 3.06).  We get a Null exception thrown from SetFontAndSize operator.  It looks like "resources" is null.
>> 
>> PDFont font = context.getResources().getFont(fontName);
>> 
>> 
>> Tracing through the code in V2, I see that PDFStreamEngine.processOperator() used to call processor.setContext(this).  That sets the context.resources to a non null value.
>> 
>> In V3, there is no call to processor.SetContext(this).
>> 
>> Can anyone the context of why this was removed, and the expectations for user code to resolve the null reference?
>> 
>> Many thanks
>> 
>> Mark
>> 

Re: Extending PDFTextStripper - Worked in v2, NullRefException in v3

Posted by Mark Gibson <ma...@staff.bluematrix.com>.
I’ll see if I can test later tonight.  Thanks for the quick response Tilman.

> On 27 Nov 2025, at 20:41, Tilman Hausherr <TH...@t-online.de> wrote:
> 
> [EXTERNAL]
> 
> In 3.0 the context is set in the constructor. But your problem is a
> different one, that "resources" is null.
> It would be interesting to find out whether this happens in ordinary
> text extraction. If not, please share minimal code that reproduces this,
> and the PDF where it happens (or does it happen with any PDF?)
> Tilman
> 
>> Am 27.11.2025 um 21:28 schrieb Mark Gibson:
>> Hi
>> 
>> We have some old code that extended PDFTextStripper using PDFBox v2.  Recently tried to upgrade to PDFBox v3 (tried 3.03 and 3.06).  We get a Null exception thrown from SetFontAndSize operator.  It looks like "resources" is null.
>> 
>> PDFont font = context.getResources().getFont(fontName);
>> 
>> 
>> Tracing through the code in V2, I see that PDFStreamEngine.processOperator() used to call processor.setContext(this).  That sets the context.resources to a non null value.
>> 
>> In V3, there is no call to processor.SetContext(this).
>> 
>> Can anyone the context of why this was removed, and the expectations for user code to resolve the null reference?
>> 
>> Many thanks
>> 
>> Mark
>> 

Re: Extending PDFTextStripper - Worked in v2, NullRefException in v3

Posted by Tilman Hausherr <TH...@t-online.de>.
In 3.0 the context is set in the constructor. But your problem is a 
different one, that "resources" is null.
It would be interesting to find out whether this happens in ordinary 
text extraction. If not, please share minimal code that reproduces this, 
and the PDF where it happens (or does it happen with any PDF?)
Tilman

Am 27.11.2025 um 21:28 schrieb Mark Gibson:
> Hi
>
> We have some old code that extended PDFTextStripper using PDFBox v2.  Recently tried to upgrade to PDFBox v3 (tried 3.03 and 3.06).  We get a Null exception thrown from SetFontAndSize operator.  It looks like "resources" is null.
>
> PDFont font = context.getResources().getFont(fontName);
>
>
> Tracing through the code in V2, I see that PDFStreamEngine.processOperator() used to call processor.setContext(this).  That sets the context.resources to a non null value.
>
> In V3, there is no call to processor.SetContext(this).
>
> Can anyone the context of why this was removed, and the expectations for user code to resolve the null reference?
>
> Many thanks
>
> Mark
>