On Mon, 16 Jun 2025 05:24:23 GMT, Prasanta Sadhukhan <psadhuk...@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 one 
> additional commit since the last revision:
> 
>   javadoc

The `ImageIcon` constructor that accepts `Image` has the same problem, or a 
similar problem.

https://github.com/openjdk/jdk/blob/9d060574e5dbd13e634f00d749d0108ceff1fae8/src/java.desktop/share/classes/javax/swing/ImageIcon.java#L224-L226

It throws `NullPointerException` if a `null` parameter is passed. Other 
constructors handle `null` values, so you should unify handling of `null` 
images.

test/jdk/javax/swing/ImageIcon/ImageIconNullImageTest.java line 40:

> 38:             ImageIcon icon = new ImageIcon();
> 39:             icon.setImage(null);
> 40:         });

`invokeAndWait` isn't needed here, you can run the test code on the main thread.

A comment that no NPE is expected would be good to clarify what the test 
expects.

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

Changes requested by aivanov (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/25767#pullrequestreview-2932316002
PR Review Comment: https://git.openjdk.org/jdk/pull/25767#discussion_r2150117727

Reply via email to