On Fri, 20 Jun 2025 03:51:16 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:
> 
>   Remove resetting description from constructor as it is already null

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

> 231:         Object o = image.getProperty("comment", imageObserver);
> 232:         if (o instanceof String) {
> 233:             description = (String) o;

To address [Phil's 
comment](https://github.com/openjdk/jdk/pull/25767/files#r2155610976):

> This is wasted work if the app calls ImageIcon(Image, String) because that 
> promptly over-writes whatever was obtained via this code.

If we're going to change the constructors, to avoid *this wasted work* when 
`ImageIcon(Image, String)` constructor is called, I suggest moving the work 
into `ImageIcon(Image, String)` and implement `ImageIcon` like this:


    public ImageIcon (Image image) {
        String description = null;
        if (image != null) {
            Object o = image.getProperty("comment", null);
            if (o instanceof String) {
                description = (String) o;
            }
        }
        this(image, description);

It is allowed in JDK 22 and later.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/25767#discussion_r2159249820

Reply via email to