RFR: 8159055: ImageIcon setImage and constructor can't handle null parameter [v9]

Phil Race prr at openjdk.org
Wed Jun 18 22:27:31 UTC 2025


On Tue, 17 Jun 2025 10:08:49 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 one additional commit since the last revision:
> 
>   SUmmary fix

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

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

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

Also that other constructor (which github won't let me comment on) installs the description even if the image is null.
That seems to be inconsistent with everything else that has a null description for a null image.

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

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


More information about the client-libs-dev mailing list