RFR: 8159055: ImageIcon.setImage and ImageIcon(Image) constructor can't handle null parameter [v10]

ExE Boss duke at openjdk.org
Thu Jun 19 04:16:34 UTC 2025


On Thu, 19 Jun 2025 03:01:15 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:

>> When trying to call 'icon.setImage(null);' where 'icon' is an instance of ImageIcon, a null pointer exception is thrown at runtime.
>> The code tried to get the `id` for that image and instantiates `MediaTracker` to associate the null image to that `id` and checks the status of loading this null image, removes the null image from the tracker and then tries to get the image width where it throws NPE as image is null.
>> 
>> It's better to not go through all MediaTracker usage and bail out initially itself for null image..
>
> Prasanta Sadhukhan has updated the pull request incrementally with two additional commits since the last revision:
> 
>  - Test fix
>  - javadoc wording..clear image desscription if image is null

These are not needed as the initial value of `description` is `null`:

src/java.desktop/share/classes/javax/swing/ImageIcon.java line 147:

> 145:         image = Toolkit.getDefaultToolkit().getImage(filename);
> 146:         if (image == null) {
> 147:             this.description = null;

Suggestion:

src/java.desktop/share/classes/javax/swing/ImageIcon.java line 187:

> 185:         image = Toolkit.getDefaultToolkit().getImage(location);
> 186:         if (image == null) {
> 187:             this.description = null;

Suggestion:

src/java.desktop/share/classes/javax/swing/ImageIcon.java line 232:

> 230:         this.image = image;
> 231:         if (image == null) {
> 232:             this.description = null;

Suggestion:

src/java.desktop/share/classes/javax/swing/ImageIcon.java line 258:

> 256:         this.image = Toolkit.getDefaultToolkit().createImage(imageData);
> 257:         if (image == null) {
> 258:             this.description = null;

Suggestion:

src/java.desktop/share/classes/javax/swing/ImageIcon.java line 284:

> 282:         this.image = Toolkit.getDefaultToolkit().createImage(imageData);
> 283:         if (image == null) {
> 284:             this.description = null;

Suggestion:

-------------

PR Review: https://git.openjdk.org/jdk/pull/25767#pullrequestreview-2941485697
PR Review Comment: https://git.openjdk.org/jdk/pull/25767#discussion_r2156059910
PR Review Comment: https://git.openjdk.org/jdk/pull/25767#discussion_r2156059678
PR Review Comment: https://git.openjdk.org/jdk/pull/25767#discussion_r2156059447
PR Review Comment: https://git.openjdk.org/jdk/pull/25767#discussion_r2156059116
PR Review Comment: https://git.openjdk.org/jdk/pull/25767#discussion_r2156059228


More information about the client-libs-dev mailing list