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

Graphics2D.getScaledInstance() makes a bad instance (drawImage NullExceptions)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.0
    • client-libs
    • 2d
    • x86
    • windows_nt



      Name: rm29839 Date: 05/28/98


      Ever play that game called "Wack A Mole"? That's what I seem to be doing tonight...

      If I load a JFIF JPEG into a BufferedImage via JPEGImageDecoder.getBufferedImage() and then call
      BufferedImage.getScaledInstance( x, y, Image.SCALE_DEFAULT) to scale it I get an Image that when passed to a
      Graphics2D.drawImage() causes a NullPointerException.

            file = new File(SourceDir.getPath()+File.separatorChar+files[i]);

            if ((file == null) || (!file.exists())) {
      continue;
            }

      is = new FileInputStream( file);

      try {
      input = new JPEGImageDecoder(is);
      src = input.decodeBufferedImage();
      } catch (Exception e) {
      System.out.println( "Bad picture: "+files[i]);
      continue;
      }

      scaled = src.getScaledInstance( tw, th, Image.SCALE_DEFAULT);
      if (scaled != null) {
      g2.drawImage( scaled, tx, ty, null);
      }

      Yeah, I know, the null looks pretty ugly in that drawImage call. But it works when you pass in a Image loaded via
      Toolkit.getDefaultToolkit().getImage()... And works with a BufferedImage (except for the reported bug there...)

      I get the follwing:
      java.lang.NullPointerException:
              at java.awt.image.FilteredImageSource.startProduction(FilteredImageSourc
      e.java:122)
              at sun.awt.image.ImageRepresentation.startProduction(ImageRepresentation
      .java:563)
              at sun.awt.image.ImageRepresentation.drawToBufImage(ImageRepresentation.
      java:633)
              at sun.awt.image.BufferedImageGraphics2D.drawImage(BufferedImageGraphics
      2D.java:432)
              at medusa.main(medusa.java:156)

      Hmm, that null does look bad doesn't it?

      Well I just went and checked it with an ImageObserver passed in instead of a null. I get the exact same result.
      So it isn't the null (and I hope that keeps working in future versions, I detest ImageObservers)

      Hmm, just checked the FilteredImageSource.java:122 reference. Looks like this:
          public void startProduction(ImageConsumer ic) {
      if (proxies == null) {
      proxies = new Hashtable();
      }
      ImageFilter imgf = (ImageFilter) proxies.get(ic);
      if (imgf == null) {
      imgf = filter.getFilterInstance(ic);
      proxies.put(ic, imgf);
      }
      src.startProduction(imgf);
          }

      No one is checking that src is non-null. The constructor isn't checking either:
          /**
           * Constructs an ImageProducer object from an existing ImageProducer
           * and a filter object.
           * @see ImageFilter
           * @see java.awt.Component#createImage
           */
          public FilteredImageSource(ImageProducer orig, ImageFilter imgf) {
      src = orig;
      filter = imgf;
          }

      This would be a good place for a coupla assert's to nail this sucker back where the real bug is...
      (Review ID: 30526)
      ======================================================================

            jehung Jeannette Hung (Inactive)
            rmandelsunw Ronan Mandel (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: