11

With ImageIO.write() API call, I get NullPointerException when I pass a non-existent path like "\\abc\abc.png". I pass the non-existent path purposely to test something but instead of getting FileNotFoundException, I get NPE. Why is that?

ImageIO.write() API is supposed to throw IOException but don't why I get NPE.

I use exception message string to show it in a message box to user but in this case NPE.getLocalizedMessage() returns empty string and hence the popup is empty with just an icon on it.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Nayan Soni
  • 1,001
  • 3
  • 12
  • 22
  • Share the code where this exception occurs please – maksimov Jun 22 '12 at 09:07
  • 3
    There is no ImageIO.write() method taking a String path as argument. Show us your code, and the stack trace of the exception. – JB Nizet Jun 22 '12 at 09:08
  • Also, you could even show your `stacktrace`. – Kazekage Gaara Jun 22 '12 at 09:15
  • General tips. 1) Even if you can find a J2SE method that accepts a String to represent a file path or URL, do not use it, instead use the overloaded variants that accept a `File` or `URL`. 2) If using a `File`, get it from a `JFileChooser`. Easier for the user and more reliable. 3) `"\\abc\abc.png"` is not a valid file path on ***any*** OS, & it seems to me it would not compile. Stop wasting your time (& more importantly ours) by typing 'something like' the code used. For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jun 22 '12 at 09:19
  • @AndrewThompson - I am not interested in wasting mine or anyone else's time.. Not my fault if you could not understand the question like Peter did. However, I found the reason/solution to my query. Thanks for being rude. – Nayan Soni Jun 22 '12 at 11:00
  • *"I am not interested in wasting mine or anyone else's time.."* The proof is in the doing, rather than the saying. Try to remember that in future. Your question was shoddily researched & expressed, as suggested by the 2 *other* down votes it earned. – Andrew Thompson Jun 22 '12 at 11:04

2 Answers2

17

He is right, though. For example, this code:

public static void main(String[] args) throws IOException {
 BufferedImage image = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
 File out = new File("\\\\ABC\\abc.png");
 ImageIO.write(image, "png", out);
}

gives

java.io.FileNotFoundException: \\ABC\abc.png (The network path was not found)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:233)
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)
at javax.imageio.ImageIO.write(ImageIO.java:1530)
at javaapplication145.JavaApplication145.main(JavaApplication145.java:24)
Exception in thread "main" java.lang.NullPointerException
at javax.imageio.ImageIO.write(ImageIO.java:1538)
at javaapplication145.JavaApplication145.main(JavaApplication145.java:24)

The reason is that FileImageOutputStreamSpi.createOutputStreamInstance swallows the FileNotFoundException and then the NPE comes when ImageIO.write tries to close a stream that didn't open.

Why the exception is suppressed so brutally, I don't know. The code fragment is

try {
 return new FileImageOutputStream((File)output);
} catch (Exception e) {
 e.printStackTrace();
 return null;
}

The only solution is to verify the path before attempting to use ImageIO.

Peter Hull
  • 6,683
  • 4
  • 39
  • 48
2

I found out the reason for NPE for issue mentioned in this thread. Peter Hull is absolutely right in saying

public static void main(String[] args) throws IOException {  BufferedImage image = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);  File out = new File("\\\\ABC\\abc.png");  ImageIO.write(image, "png", out); }

This is exactly my code looks like. Thanks Peter for highlighting.

The reason for this issue is that new FileImageOutputStream() throws a FileNotFoundException but some Sun programmer went and caught the exception, printed the stack trace, and returned null. Which is why it's no longer possible to catch the FileNotFoundException - it's already printed. Shortly afterwards, the returned null value causes a NullPointerException, which is what it being thrown from the method I called. When printed the Stack Trace of the exception, I could see FileNotFoundException along with NPE for the reason mentioned above.

-Nayan

Nayan Soni
  • 1,001
  • 3
  • 12
  • 22