You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Puhong You (JIRA)" <ji...@apache.org> on 2013/12/06 21:43:35 UTC

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

Puhong You created PDFBOX-1799:
----------------------------------

             Summary: 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


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, icon);

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 to 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#6144)