PDFBox
  1. PDFBox
  2. PDFBOX-1276

java.lang.NullPointerException on trying to set value for PDTextBox in pdf file.

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: 1.6.0
    • Fix Version/s: 2.0.0
    • Component/s: AcroForm
    • Labels:
    • Environment:
      OS : Windows 7
      JVM : 1.7.0_02 [64-bit]

      Description

      When trying to set value for a field in pdf using,

      PDDocument pdf = PDDocument.load(<pdfFileName>);
      PDDocumentCatalog docCatalog = pdf.getDocumentCatalog();
      PDAcroForm acroForm = docCatalog.getAcroForm();
      PDTextbox t = (PDTextbox) acroForm.getField(<fieldName>);
      t.setValue(<someValue>);

      got following exception :

      java.lang.NullPointerException
      at org.apache.pdfbox.pdmodel.common.COSDictionaryMap.put(COSDictionaryMap.java:106)
      at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.getFontAndUpdateResources(PDAppearance.java:440)
      at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.setAppearanceValue(PDAppearance.java:268)
      at org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.setValue(PDVariableText.java:131)
      at qs.common.GenerateReports.generateBidderDeclaration_OBS_05_105(GenerateReports.java:71)

      Source of the exception :

      NullPointerException thrown at retval = (PDFont)formResources.getFonts().get( fontName ); in org.apache.pdfbox.pdmodel.interactive.form.PDAppearance

      Tried suggestion from following link :

      http://mail-archives.apache.org/mod_mbox/pdfbox-dev/201108.mbox/%3CBE54CE7A716A7247B1D3BF9D9C187CED0843E2A8@SOM-TEAQASMAIL1.som.w2k.state.me.us%3E

      i.e. intruduced a try-catch for

      retval = (PDFont)formResources.getFonts().get( fontName );

      resolved the issue for me as well.

      Also, I had to add try-catch for following :

      printWriter.println(getTextPosition(boundingBox, pdFont, fontSize, tokens));

      as getTextPosition() is throwing IOException as the pdFont is null.

      Creating this issue so that the bug is documented and would like to see the change in upcoming releases.

      Shall attach the jar file that contains the changes which resolved the issue.

      1. Bug 1276.patch
        0.6 kB
        Manoj Patel
      2. pdfbox-app-1.6.0_changed_null_check.jar
        9.83 MB
        Ravikiran Mane
      3. pdfbox-app-1.6.0_changed.jar
        9.83 MB
        Ravikiran Mane

        Activity

        Ravikiran Mane created issue -
        Ravikiran Mane made changes -
        Field Original Value New Value
        Description When trying to set value for a field in pdf, got following exception :

        java.lang.NullPointerException
        at org.apache.pdfbox.pdmodel.common.COSDictionaryMap.put(COSDictionaryMap.java:106)
        at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.getFontAndUpdateResources(PDAppearance.java:440)
        at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.setAppearanceValue(PDAppearance.java:268)
        at org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.setValue(PDVariableText.java:131)
        at qs.common.GenerateReports.generateBidderDeclaration_OBS_05_105(GenerateReports.java:71)

        Hence after following suggestion from following link :

        http://mail-archives.apache.org/mod_mbox/pdfbox-dev/201108.mbox/%3CBE54CE7A716A7247B1D3BF9D9C187CED0843E2A8@SOM-TEAQASMAIL1.som.w2k.state.me.us%3E

        i.e. intruduced a try-catch for

        retval = (PDFont)formResources.getFonts().get( fontName );

        resolved the issue for me as well.

        Creating this issue so that the bug is documented and would like to see the change in upcoming releases.

        Shall attach the jar file that contains the changes which resolved the issue.
        When trying to set value for a field in pdf, got following exception :

        java.lang.NullPointerException
        at org.apache.pdfbox.pdmodel.common.COSDictionaryMap.put(COSDictionaryMap.java:106)
        at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.getFontAndUpdateResources(PDAppearance.java:440)
        at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.setAppearanceValue(PDAppearance.java:268)
        at org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.setValue(PDVariableText.java:131)
        at qs.common.GenerateReports.generateBidderDeclaration_OBS_05_105(GenerateReports.java:71)

        Hence after following suggestion from following link :

        http://mail-archives.apache.org/mod_mbox/pdfbox-dev/201108.mbox/%3CBE54CE7A716A7247B1D3BF9D9C187CED0843E2A8@SOM-TEAQASMAIL1.som.w2k.state.me.us%3E

        i.e. intruduced a try-catch for

        retval = (PDFont)formResources.getFonts().get( fontName );

        resolved the issue for me as well.

        Also, I had to add try-catch for following :

        printWriter.println(getTextPosition(boundingBox, pdFont, fontSize, tokens));

        as getTextPosition() is throwing IOException as the font is null.

        Creating this issue so that the bug is documented and would like to see the change in upcoming releases.

        Shall attach the jar file that contains the changes which resolved the issue.
        Ravikiran Mane made changes -
        Attachment pdfbox-app-1.6.0.jar [ 12521062 ]
        Ravikiran Mane made changes -
        Attachment pdfbox-app-1.6.0.jar [ 12521062 ]
        Hide
        Ravikiran Mane added a comment - - edited

        Please note that the changes won't block the exception rather would allow creation of pdf inspite of exceptions due to font.

        Show
        Ravikiran Mane added a comment - - edited Please note that the changes won't block the exception rather would allow creation of pdf inspite of exceptions due to font.
        Ravikiran Mane made changes -
        Attachment pdfbox-app-1.6.0_changed.jar [ 12521063 ]
        Ravikiran Mane made changes -
        Comment [ Note the changes would not block the exceptions thrown however it allows creation of pdf inspite to Exceptions. ]
        Ravikiran Mane made changes -
        Description When trying to set value for a field in pdf, got following exception :

        java.lang.NullPointerException
        at org.apache.pdfbox.pdmodel.common.COSDictionaryMap.put(COSDictionaryMap.java:106)
        at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.getFontAndUpdateResources(PDAppearance.java:440)
        at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.setAppearanceValue(PDAppearance.java:268)
        at org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.setValue(PDVariableText.java:131)
        at qs.common.GenerateReports.generateBidderDeclaration_OBS_05_105(GenerateReports.java:71)

        Hence after following suggestion from following link :

        http://mail-archives.apache.org/mod_mbox/pdfbox-dev/201108.mbox/%3CBE54CE7A716A7247B1D3BF9D9C187CED0843E2A8@SOM-TEAQASMAIL1.som.w2k.state.me.us%3E

        i.e. intruduced a try-catch for

        retval = (PDFont)formResources.getFonts().get( fontName );

        resolved the issue for me as well.

        Also, I had to add try-catch for following :

        printWriter.println(getTextPosition(boundingBox, pdFont, fontSize, tokens));

        as getTextPosition() is throwing IOException as the font is null.

        Creating this issue so that the bug is documented and would like to see the change in upcoming releases.

        Shall attach the jar file that contains the changes which resolved the issue.
        When trying to set value for a field in pdf, got following exception :

        java.lang.NullPointerException
        at org.apache.pdfbox.pdmodel.common.COSDictionaryMap.put(COSDictionaryMap.java:106)
        at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.getFontAndUpdateResources(PDAppearance.java:440)
        at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.setAppearanceValue(PDAppearance.java:268)
        at org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.setValue(PDVariableText.java:131)
        at qs.common.GenerateReports.generateBidderDeclaration_OBS_05_105(GenerateReports.java:71)

        Hence after following suggestion from following link :

        http://mail-archives.apache.org/mod_mbox/pdfbox-dev/201108.mbox/%3CBE54CE7A716A7247B1D3BF9D9C187CED0843E2A8@SOM-TEAQASMAIL1.som.w2k.state.me.us%3E

        i.e. intruduced a try-catch for

        retval = (PDFont)formResources.getFonts().get( fontName );

        resolved the issue for me as well.

        Also, I had to add try-catch for following :

        printWriter.println(getTextPosition(boundingBox, pdFont, fontSize, tokens));

        as getTextPosition() is throwing IOException as the pdFont is null.

        Creating this issue so that the bug is documented and would like to see the change in upcoming releases.

        Shall attach the jar file that contains the changes which resolved the issue.
        Hide
        Ravikiran Mane added a comment -

        After further working on the issue found the following :

        Alternatively, we can get rid of the exception by confirming if retval and pdFont are not null before the following :

        .* 440 */ : streamResources.getFonts().put(fontName, retval); and

        /* 380 */ : printWriter.println(getTextPosition(boundingBox, pdFont, fontSize, tokens));

        Please do let me know if this is acceptable.

        Shall attach the jarcontaining alternative changes to get rid of the exceptions as well.

        Thanks & Regards,

        Show
        Ravikiran Mane added a comment - After further working on the issue found the following : Alternatively, we can get rid of the exception by confirming if retval and pdFont are not null before the following : .* 440 */ : streamResources.getFonts().put(fontName, retval); and /* 380 */ : printWriter.println(getTextPosition(boundingBox, pdFont, fontSize, tokens)); Please do let me know if this is acceptable. Shall attach the jarcontaining alternative changes to get rid of the exceptions as well. Thanks & Regards,
        Hide
        Ravikiran Mane added a comment - - edited

        The file "pdfbox-app-1.6.0_changed_null_check.jar" contains changes that get rid of the exceptions as well.

        Show
        Ravikiran Mane added a comment - - edited The file "pdfbox-app-1.6.0_changed_null_check.jar" contains changes that get rid of the exceptions as well.
        Ravikiran Mane made changes -
        Attachment pdfbox-app-1.6.0_changed_null_check.jar [ 12521071 ]
        Ravikiran Mane made changes -
        Summary NullPointerException thrown at retval = (PDFont)formResources.getFonts().get( fontName ); in org.apache.pdfbox.pdmodel.interactive.form.PDAppearance java.lang.NullPointerException on trying to set value for PDTextBox in pdf file.
        Description When trying to set value for a field in pdf, got following exception :

        java.lang.NullPointerException
        at org.apache.pdfbox.pdmodel.common.COSDictionaryMap.put(COSDictionaryMap.java:106)
        at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.getFontAndUpdateResources(PDAppearance.java:440)
        at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.setAppearanceValue(PDAppearance.java:268)
        at org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.setValue(PDVariableText.java:131)
        at qs.common.GenerateReports.generateBidderDeclaration_OBS_05_105(GenerateReports.java:71)

        Hence after following suggestion from following link :

        http://mail-archives.apache.org/mod_mbox/pdfbox-dev/201108.mbox/%3CBE54CE7A716A7247B1D3BF9D9C187CED0843E2A8@SOM-TEAQASMAIL1.som.w2k.state.me.us%3E

        i.e. intruduced a try-catch for

        retval = (PDFont)formResources.getFonts().get( fontName );

        resolved the issue for me as well.

        Also, I had to add try-catch for following :

        printWriter.println(getTextPosition(boundingBox, pdFont, fontSize, tokens));

        as getTextPosition() is throwing IOException as the pdFont is null.

        Creating this issue so that the bug is documented and would like to see the change in upcoming releases.

        Shall attach the jar file that contains the changes which resolved the issue.
        When trying to set value for a field in pdf using,
          
         PDDocument pdf = PDDocument.load(<pdfFileName>);
         PDDocumentCatalog docCatalog = pdf.getDocumentCatalog();
         PDAcroForm acroForm = docCatalog.getAcroForm();
         PDTextbox t = (PDTextbox) acroForm.getField(<fieldName>);
                          t.setValue(<someValue>);

        got following exception :

        java.lang.NullPointerException
        at org.apache.pdfbox.pdmodel.common.COSDictionaryMap.put(COSDictionaryMap.java:106)
        at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.getFontAndUpdateResources(PDAppearance.java:440)
        at org.apache.pdfbox.pdmodel.interactive.form.PDAppearance.setAppearanceValue(PDAppearance.java:268)
        at org.apache.pdfbox.pdmodel.interactive.form.PDVariableText.setValue(PDVariableText.java:131)
        at qs.common.GenerateReports.generateBidderDeclaration_OBS_05_105(GenerateReports.java:71)

        Source of the exception :

        NullPointerException thrown at retval = (PDFont)formResources.getFonts().get( fontName ); in org.apache.pdfbox.pdmodel.interactive.form.PDAppearance

        Tried suggestion from following link :

        http://mail-archives.apache.org/mod_mbox/pdfbox-dev/201108.mbox/%3CBE54CE7A716A7247B1D3BF9D9C187CED0843E2A8@SOM-TEAQASMAIL1.som.w2k.state.me.us%3E

        i.e. intruduced a try-catch for

        retval = (PDFont)formResources.getFonts().get( fontName );

        resolved the issue for me as well.

        Also, I had to add try-catch for following :

        printWriter.println(getTextPosition(boundingBox, pdFont, fontSize, tokens));

        as getTextPosition() is throwing IOException as the pdFont is null.

        Creating this issue so that the bug is documented and would like to see the change in upcoming releases.

        Shall attach the jar file that contains the changes which resolved the issue.
        Hide
        Andreas Lehmkühler added a comment -

        Please provide a patch using a diff format and remove the attached source jars.

        Show
        Andreas Lehmkühler added a comment - Please provide a patch using a diff format and remove the attached source jars.
        Andreas Lehmkühler made changes -
        Fix Version/s 1.6.0 [ 12316242 ]
        Hide
        Ravikiran Mane added a comment -

        Hello Andreas,

        I shall remove the jar files once the patch is ready.

        It would be great if you could provide me link that explains generation of "diff" format patches. In the meanwhile, I shall be trying it myself.

        I am new to the open source community. Kindly bear with me.

        Thanks & Regards,
        Ravikiran Mane.

        Show
        Ravikiran Mane added a comment - Hello Andreas, I shall remove the jar files once the patch is ready. It would be great if you could provide me link that explains generation of "diff" format patches. In the meanwhile, I shall be trying it myself. I am new to the open source community. Kindly bear with me. Thanks & Regards, Ravikiran Mane.
        Hide
        Andreas Lehmkühler added a comment -

        We are using subversion [1] as version control system. Here is a quick and rough description on how to create a diff:

        • install a subversion client [2]
        • create a directory: mkdir pdfbox
        • change to the directory: cd pdfbox
        • checkout the current trunk version of PDFBox: svn checkout http://svn.apache.org/repos/asf/pdfbox/trunk
        • apply your changes to the code in that directory
        • run the following command to create a patch using the diff format: svn diff > patch.diff

        Join our mailing lists [3] if you need further information,

        [1] http://subversion.apache.org
        [2] http://subversion.apache.org/packages.html
        [3] http://pdfbox.apache.org/mail-lists.html

        Show
        Andreas Lehmkühler added a comment - We are using subversion [1] as version control system. Here is a quick and rough description on how to create a diff: install a subversion client [2] create a directory: mkdir pdfbox change to the directory: cd pdfbox checkout the current trunk version of PDFBox: svn checkout http://svn.apache.org/repos/asf/pdfbox/trunk apply your changes to the code in that directory run the following command to create a patch using the diff format: svn diff > patch.diff Join our mailing lists [3] if you need further information, [1] http://subversion.apache.org [2] http://subversion.apache.org/packages.html [3] http://pdfbox.apache.org/mail-lists.html
        Hide
        Ravikiran Mane added a comment -

        Hello Andreas,

        Thank you for the information.

        Shall try the same and upload the patch shortly.

        Thanks & Regards,
        Ravikiran Mane.

        Show
        Ravikiran Mane added a comment - Hello Andreas, Thank you for the information. Shall try the same and upload the patch shortly. Thanks & Regards, Ravikiran Mane.
        Hide
        Manoj Patel added a comment -

        I found same issue and changed code in PDAppearance as
        if( retval == null )
        {
        retval = (PDFont)formResources.getFonts().get( fontName );
        //Bug Fix - START
        if(retval == null)

        { retval = PDType1Font.HELVETICA; }

        //Bug Fix - End
        streamResources.getFonts().put( fontName, retval );
        }

        It's working fine for me.

        Show
        Manoj Patel added a comment - I found same issue and changed code in PDAppearance as if( retval == null ) { retval = (PDFont)formResources.getFonts().get( fontName ); //Bug Fix - START if(retval == null) { retval = PDType1Font.HELVETICA; } //Bug Fix - End streamResources.getFonts().put( fontName, retval ); } It's working fine for me.
        Hide
        Manoj Patel added a comment -

        Try Bug 1276.patch as i have mentioned and give your comments whether this patch can solve your issue or not.

        Show
        Manoj Patel added a comment - Try Bug 1276.patch as i have mentioned and give your comments whether this patch can solve your issue or not.
        Manoj Patel made changes -
        Attachment Bug 1276.patch [ 12528304 ]
        John Hewson made changes -
        Component/s AcroForm [ 12312225 ]
        Component/s PDModel [ 12312224 ]
        Maruan Sahyoun made changes -
        Assignee Maruan Sahyoun [ msahyoun ]
        Maruan Sahyoun made changes -
        Fix Version/s 2.0.0 [ 12319281 ]
        Hide
        Maruan Sahyoun added a comment -

        Duplicate of PDFBOX-1550

        Show
        Maruan Sahyoun added a comment - Duplicate of PDFBOX-1550
        Maruan Sahyoun made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Resolution Duplicate [ 3 ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        1037d 11m 1 Maruan Sahyoun 03/Feb/15 21:43

          People

          • Assignee:
            Maruan Sahyoun
            Reporter:
            Ravikiran Mane
          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development