With the API call "ImageIO.write ()" I get a NullPointerException

With the ImageIO.write() API call, I get a NullPointerException when I pass a nonexistent path, like "\\abc\abc.png" . I pass a nonexistent path specifically to test something, but instead of getting a FileNotFoundException I get an NPE . Why is this?

ImageIO.write() API should throw an IOException , but why not get an NPE .

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

+6
source share
2 answers

He's right. 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 a FileNotFoundException and then the NPE comes in when ImageIO.write tries to close a stream that doesn't open.

Why the exception is suppressed so cruelly, I do not know. Code snippet

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

The only solution is to check the path before trying to use ImageIO.

+12
source

I found out the cause of NPE for the problem 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 what my code looks like. Thanks to Peter for your attention.

The reason for this problem is that the new FileImageOutputStream () throws a FileNotFoundException exception, but some Sun programmer went and caught the exception, printed a stack trace and returned null. That's why it is no longer possible to catch a FileNotFoundException - it is already printed. Shortly after this, the null return throws a NullPointerException, which is that it is thrown from the method that I called. When you threw Stack Trace exceptions, I could see a FileNotFoundException along with an NPE for the reason mentioned above.

-Nayan

+1
source

Source: https://habr.com/ru/post/918735/


All Articles