Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8140536

NPE in ImageIO class write method line 1538 while closing the output stream

    XMLWordPrintable

    Details

      Description

      FULL PRODUCT VERSION :
      C:\Users\reynaldo>java -version
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      null pointer exception in the finally statement of the javax.imageio.ImageIO.write(ImageIO.java:1538) while trying to write an image to the file system because the stream is null.
       
            try {
                  return doWrite(im, writer, stream);
              } finally {
                  stream.close(); //<--- NPE here
              }

      ImageIO.class full code:

          /**
           * Writes an image using an arbitrary <code>ImageWriter</code>
           * that supports the given format to a <code>File</code>. If
           * there is already a <code>File</code> present, its contents are
           * discarded.
           *
           * @param im a <code>RenderedImage</code> to be written.
           * @param formatName a <code>String</code> containing the informal
           * name of the format.
           * @param output a <code>File</code> to be written to.
           *
           * @return <code>false</code> if no appropriate writer is found.
           *
           * @exception IllegalArgumentException if any parameter is
           * <code>null</code>.
           * @exception IOException if an error occurs during writing.
           */
          public static boolean write(RenderedImage im,
                                      String formatName,
                                      File output) throws IOException {
              if (output == null) {
                  throw new IllegalArgumentException("output == null!");
              }
              ImageOutputStream stream = null;

              ImageWriter writer = getWriter(im, formatName);
              if (writer == null) {
                  /* Do not make changes in the file system if we have
                   * no appropriate writer.
                   */
                  return false;
              }

              try {
                  output.delete();
                  stream = createImageOutputStream(output);
              } catch (IOException e) {
                  throw new IIOException("Can't create output stream!", e);
              }

              try {
                  return doWrite(im, writer, stream);
              } finally {
                  stream.close();
              }
          }

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      write image to file system.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Image should be written to hard drive
      ACTUAL -
      NPE thrown, no image written to disk

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      at java.lang.Thread.run(Thread.java:745)
      Caused by: java.lang.NullPointerException
      at javax.imageio.ImageIO.write(ImageIO.java:1538)
      at server.data.Store.writeImage(Store.java:257)

      REPRODUCIBILITY :
      This bug can be reproduced occasionally.

      ---------- BEGIN SOURCE ----------
      public static boolean writeImage(String id, BufferedImage img) {
      boolean result = false;
      if (img==null) return result;
      String loc = base + File.separator + "jpg";
      File dir = new File(loc);
      if (!dir.exists() || !dir.isDirectory()) {
      dir.mkdirs();
      }
      String fil = loc + File.separator + id + ".jpg";
      File src = new File(fil);
      try {
      result = ImageIO.write(img, "JPG", src);
      } catch (IOException e) {
      Log.log("Error","error writing image "+ fil,e);
      }
      return result;
      }
      ---------- END SOURCE ----------

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: