-
CSR
-
Resolution: Unresolved
-
P4
-
None
-
behavioral
-
minimal
-
Creating ImageIcon(Image) with a null image or setting a null image with the setImage method will no longer throw NPE, no icon will be rendered.
-
Java API
-
JDK
Summary
If either ImageIcon.setImage(Image)
or ImageIcon(Image)
receives a null
image as a parameter, the image should be set to null
and no icon is rendered.
Problem
The ImageIcon.setImage
method receives an Image
object as a parameter. If it is null
, the method throws NullPointerException
from the loadImage
method after it creates MediaTracker
to track loading the image, which is unnecessary since the image is null
. Similarly, the ImageIcon(Image)
constructor throws NullPointerException
if the Image
object is null
.
Solution
Set the current image to null
and return early if a null
image is passed to the ImageIcon(Image)
constructor and the setImage(Image)
method. A null
image renders no icon.
Specification
javax.swing.ImageIcon
/**
* Creates an ImageIcon from an image object.
+ * If the image is {@code null}, no icon will be rendered.
* If the image has a "comment" property that is a string,
public ImageIcon (Image image) {
/**
* Sets the image displayed by this icon.
+ * Setting a {@code null} image means
+ * any existing icon will be removed
+ * and no icon will be rendered.
* @param image the image
*/
public void setImage(Image image) {
- csr of
-
JDK-8159055 ImageIcon.setImage and ImageIcon(Image) constructor can't handle null parameter
-
- Open
-