You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Andreas Lehmkühler (JIRA)" <ji...@apache.org> on 2014/01/13 19:12:57 UTC

[jira] [Resolved] (PDFBOX-1799) NullPointerException when constructing a PDJPeg using a BufferedImage

     [ https://issues.apache.org/jira/browse/PDFBOX-1799?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Andreas Lehmkühler resolved PDFBOX-1799.
----------------------------------------

       Resolution: Fixed
    Fix Version/s: 2.0.0
                   1.8.4
         Assignee: Andreas Lehmkühler

I added a fix in revisions 1557793 (trunk) and 1557794 (1.8 branch) based on Puhongs proposal.

I don't like the idea of removing one specific writer. Maybe someone will repair that writer in the future, who knows ...

Thanks for the contribution!

> NullPointerException when constructing a PDJPeg using a BufferedImage
> ---------------------------------------------------------------------
>
>                 Key: PDFBOX-1799
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-1799
>             Project: PDFBox
>          Issue Type: Bug
>          Components: Utilities
>    Affects Versions: 1.8.3
>         Environment: Windows 2008 R2 Standard
>            Reporter: Puhong You
>            Assignee: Andreas Lehmkühler
>             Fix For: 1.8.4, 2.0.0
>
>
> In pdfbox 1.8.2 and 1.8.3, the constructor of PDJPeg class that takes a BufferedImage throws java.lang.NullPointerException:
> 	    BufferedImage bi = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream("images/icon.png"));
> 	    
> 	    PDJpeg icon = new PDJpeg(pdf, bi);
> The stack trace is:
> java.lang.NullPointerException
> 	at org.apache.pdfbox.util.ImageIOUtil.addResolution(ImageIOUtil.java:211)
> 	at org.apache.pdfbox.util.ImageIOUtil.createMetadata(ImageIOUtil.java:204)
> 	at org.apache.pdfbox.util.ImageIOUtil.writeImage(ImageIOUtil.java:158)
> 	at org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg.createImageStream(PDJpeg.java:171)
> 	at org.apache.pdfbox.pdmodel.graphics.xobject.PDJpeg.<init>(PDJpeg.java:133)
> The culprit is in this method in the ImageIOUtil:
>     private static IIOMetadata createMetadata(RenderedImage image, ImageWriter imageWriter,
>             ImageWriteParam writerParams, int resolution)
>     {
>         .....
>         IIOMetadata meta = imageWriter.getDefaultImageMetadata( type, writerParams );
>         return (addResolution(meta, resolution) ? meta : null);
>     } 
> One of the JPG image writer in our environment is com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriter, its implementation of getDefaultImageMetadata() returns null:
> public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType, ImageWriteParam param) {
>         return null;
>     }
> this causes the NullPointerException at the first line of the addResolution() method:
>        if (!meta.isReadOnly() && meta.isStandardMetadataFormatSupported())
> I suggest that null checks be added at the following places:
> 1. ImageIOUtil.addResolution():
>        if (!meta.isReadOnly() && meta.isStandardMetadataFormatSupported())
> ==>
>        if (meta != null && meta.isReadOnly() && meta.isStandardMetadataFormatSupported())
> 2. ImageIOUtil.writeImage():
>                    IIOMetadata meta = createMetadata( image, imageWriter, writerParams, resolution);
> 	           imageWriter.setOutput( output );
> 	           imageWriter.write( null, new IIOImage( image, null, meta ), writerParams );
> 	           foundWriter = true;
> ==>
>                    IIOMetadata meta = createMetadata( image, imageWriter, writerParams, resolution);
>                     if (meta != null) {
> 	                    imageWriter.setOutput( output );
> 	                    imageWriter.write( null, new IIOImage( image, null, meta ), writerParams );
> 	                    foundWriter = true;
>                     }



--
This message was sent by Atlassian JIRA
(v6.1.5#6160)