question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImageIO.createImageOutputStream returns null

See original GitHub issue

I am using the TwelveMonkey TIFF plugin to save TIFF files to a network share that is connected as NFS.

On one computer that I use (Windows 7) this appears to work flawlessly. However on another computer (Windows 10) I get an occasional issue where ImageIO.createImageOutputStream returns null.

Here is a snippet of the problematic code:

        File directory = new File(this.savePath);
        if (!directory.exists()) { directory.mkdirs(); }
        File file = Paths.get(this.savePath, fullFileName).toFile(); 
        try (ImageOutputStream outStream = ImageIO.createImageOutputStream(file)) {
            writer.setOutput(outStream);
            writer.prepareWriteSequence(null); //null means we will use the default streamMetadata.
        ...

It seems that for roughly 1 out of 3 runs of this code the outputStream gets returned as null. This then correctly causes an exception to be thrown at writer.setOutput(outStream.

How can I figure out why the plugin is failing to create an outputStream?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nanthony21commented, Mar 12, 2021

So, in todays testing I did in fact see a stack trace printed out and you were right about the cause.

Here is the stack trace:

java.io.FileNotFoundException: Z:\Nick\test2\Cell2\PWS\pws.tif (The system cannot find the path specified)
	at java.io.RandomAccessFile.open0(Native Method)
	at java.io.RandomAccessFile.open(RandomAccessFile.java:316)
	at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)
	at javax.imageio.stream.FileImageOutputStream.<init>(FileImageOutputStream.java:69)
	at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(FileImageOutputStreamSpi.java:55)
	at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:419)

I noticed that this only happens when there isn’t a break point that results in a pause between creating the File and calling createImageOutputStream so it’s probably something to do with timing.

I’ll have to do some more reading about RandomAccessFile, hopefully I can make the program briefly pause until the File is read to be passed in to createImageOutputStream.

Since this is clearly not related to TwelveMonkeys I’ll go ahead and close this. Thanks for the help!

1reaction
nanthony21commented, Mar 12, 2021

Thanks @haraldk , I’ll see if I can dig into what is going on in com.sun.imageio.spi.FileImageOutputStreamSpi.

When looking at the output in the debugger I didn’t see any stack traces coming up which makes me think that the null I was getting may actually come directly from ImageIO.createImageOutputStream:

        while (iter.hasNext()) {
            ImageOutputStreamSpi spi = (ImageOutputStreamSpi)iter.next();
            if (spi.getOutputClass().isInstance(output)) {
                try {
                    return spi.createOutputStreamInstance(output,
                                                          usecache,
                                                          getCacheDirectory());
                } catch (IOException e) {
                    throw new IIOException("Can't create cache file!", e);
                }
            }
        }

        return null;
Read more comments on GitHub >

github_iconTop Results From Across the Web

ImageIO.createImageInputStream is returning null, how come?
Try using an InputStream constructed with the File . ImageIO.createImageInputStream(new FileInputStream(f));. Here's a related example.
Read more >
ImageIO (Java Platform SE 7 ) - Oracle Help Center
Returns an ImageWriter corresponding to the given ImageReader , if there is one, or null if the plug-in for this ImageReader does not...
Read more >
ImageIO.createImageOutputStream - Java - Tabnine
ImageIO.createImageOutputStream (Showing top 20 results out of 1,602) ... writeToSequence(new IIOImage(image, null, null), writer.getDefaultWriteParam()) ...
Read more >
javax.imageio.ImageIO#createImageOutputStream
This page shows Java code examples of javax.imageio.ImageIO#createImageOutputStream.
Read more >
In ImageIO.write() and ImageIO.read() null stream is not ...
ImageOutputStream is created through code "stream = createImageOutputStream(output);" In the finally block stream is closed without null check
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found