Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4905411
Votes 1
Synopsis Cropping a BufferedImage image to 0 size causes NullPointerException
Category java:classes_2d
Reported Against 1.4 , 1.1.1 , 1.4.1
Release Fixed 1.5(tiger)
State 10-Fix Delivered, bug
Priority: 4-Low
Related Bugs 4780802
Submit Date 12-AUG-2003
Description




FULL PRODUCT VERSION :
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-b28)
Java HotSpot(TM) Client VM (build 1.4.2-b28, mixed mode)

jai-1_1_2-lib-linux-i586-jdk.bin installed

FULL OS VERSION :
Linux moo-hf 2.4.18 #2 SMP Mon May 20 19:08:12 EST 2002 i686 unknown


A DESCRIPTION OF THE PROBLEM :
Cropping an image to height or width of zero or less causes a NullPointerException, when attempting to draw the image or waitFor the image if the image is loaded via JAI, but not if the image is loaded via the awt Toolkit.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the attached program with the name of any image file as the parameter.eg:
java Bug *.jpg

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Four windows should open titled:
  Toolkit load
  Toolkit crop
JAI load
JAI crop
The cropped windows should be empty the load image should contain the image, if the image is supported (eg a TIFF image will appear in the JAI load window but not in the Toolkit load window). (guess who is trying to add TIFF support to an existing application).



ACTUAL -
The JAI crop window does not appear. Instead a NullPointerException is thrown.

The application works as expected if the crop parameters are all positive.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
java Bug *.jpg
Warning: Cannot convert string "-monotype-arial-regular-r-normal--*-140-*-*-p-*-iso8859-1" to type FontStruct
Exception in thread "main" java.lang.NullPointerException
        at sun.awt.image.OffScreenImageSource.sendPixels(OffScreenImageSource.java:142)
        at sun.awt.image.OffScreenImageSource.produce(OffScreenImageSource.java:161)
        at sun.awt.image.OffScreenImageSource.addConsumer(OffScreenImageSource.java:37)
        at sun.awt.image.OffScreenImageSource.startProduction(OffScreenImageSource.java:51)
        at java.awt.image.FilteredImageSource.startProduction(FilteredImageSource.java:116)
        at sun.awt.image.ImageRepresentation.startProduction(ImageRepresentation.java:644)
        at sun.awt.image.Image.addWatcher(Image.java:197)
        at sun.awt.image.Image.getProperty(Image.java:145)
        at javax.swing.ImageIcon.<init>(ImageIcon.java:161)
        at Bug.showImage(Bug.java:15)
        at Bug.main(Bug.java:47)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class Bug {

   static Component component = new JLabel("");

   public static void showImage(Image image, String title) {
      if (null == image)
	 return;

      JFrame frame = new JFrame(title);
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      JLabel label = new JLabel(new ImageIcon(image));
      frame.getContentPane().add(label);
      frame.pack();
      frame.setVisible(true);
   }

   public static Image cropImage(Image image) {
      return component.createImage(new FilteredImageSource(image.getSource(),
							   new CropImageFilter(100,
									       100,
									       0,
									       100)));
   }

   public static void main(String[] args) {

      Image image;
      Image croppedImage;


      for (int i = 0; i < args.length; i++) {
	 image = Toolkit.getDefaultToolkit().createImage(args[i]);
	 showImage(image, "Toolkit load");

	 croppedImage = cropImage(image);
	 showImage(croppedImage, "Toolkit crop");

	 image = javax.media.jai.JAI.create("fileload", args[i]).getAsBufferedImage();

	 showImage(image, "JAI load");

	 croppedImage = cropImage(image);
	 showImage(croppedImage, "JAI crop");

      }
   }
    
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Explicitly checking crop bounds before cropping.

As the cropping is done deep in a set of libraries I am using, rather in my own code, this is difficult to work around.
(Incident Review ID: 193089) 
======================================================================
Work Around
N/A
Evaluation
Image processing belongs to 2D, not AWT. 
  xxxxx@xxxxx   2003-08-12

The problem is that we do not check for (theConsumer == null) before calling
sendPixels() in OffScreenImageSource.produce().  Note that theConsumer can
become null during any of the four callbacks in the produce() method, so we
must check for null before every call.  A better approach would be to put the
four method calls in a try/catch block, and catch a NullPointerException if
theConsumer is null.  In the catch block, if theConsumer != null, then something
else caused the NPE, and we should notify theConsumer of the error (this is
something that we did not do in the past, but we should start now).
  xxxxx@xxxxx   2003-09-04
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang