JDK-7045161 : NullPointerException in javax.imageio.ImageIO.write(RenderedImage, String, File)
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.imageio
  • Affected Version: 6u25
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2011-05-16
  • Updated: 2015-09-25
  • Resolved: 2014-05-07
Related Reports
Duplicate :  
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_25-b06)
Java HotSpot(TM) Client VM (build 20.0-b11, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Linux 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:05:01 UTC 2009 x86_64 GNU/Linux
Darwin 10.7.4 Darwin Kernel Version 10.7.4: Mon Apr 18 21:24:17 PDT 2011; root:xnu-1504.14.12~3/RELEASE_X86_64 x86_64
Windows 7 Ultimate 32 bit

A DESCRIPTION OF THE PROBLEM :
When using javax.imageio.ImageIO.write(RenderedImage, String, File) with png format and a file that can not be created, a FileNotFoundException is written to System.err and a NullPointerException is thrown.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run source code attached below.
* Create a RenderedImage 'img'
* Create a File object for a non existing directory 'dir'
* Call ImageIO.write(img, "png", new File(dir, "foo.png"));

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
A IOException (or FileNotFoundException) should be thrown by Image.write() and the source code attached bolow should output "File can not be created (expected)".
ACTUAL -
A FileNotFoundException and its trace is written to System.err and a NullPointerException is thrown. The output is:
java.io.FileNotFoundException: dirDoesNotExist/foo.png (No such file or directory)
	at java.io.RandomAccessFile.open(Native Method)
	at java.io.RandomAccessFile.<init>(RandomAccessFile.java:212)
	at javax.imageio.stream.FileImageOutputStream.<init>(FileImageOutputStream.java:53)
	at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(FileImageOutputStreamSpi.java:37)
	at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:393)
	at javax.imageio.ImageIO.write(ImageIO.java:1514)
	at Bug.main(Bug.java:13)
Exception in thread "main" java.lang.NullPointerException
	at javax.imageio.ImageIO.write(ImageIO.java:1523)
	at Bug.main(Bug.java:13)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;

public class Bug
{
	public static void main(String[] args)
	{
		final BufferedImage img =
			new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
		final File dir = new File("dirDoesNotExist");

		try
		{
			ImageIO.write(img, "png", new File(dir, "foo.png"));
		}
		catch (IOException e)
		{
			System.err.println("File can not be created (expected)");
		}
	}
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
* Catch NullPointerException
* Make sure the output file can be written

Comments
Early reported bug 5034864 describes the problem more clearly: the FileNotFoundException is just printed in the spi classes, and never reach the ImageIO.write() level.
07-05-2014