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

Phil Race prr at openjdk.org
Thu Jun 19 17:03:34 UTC 2025


On Thu, 19 Jun 2025 10:08:33 GMT, Alexey Ivanov <aivanov at openjdk.org> wrote:

>>> Also that other constructor (which github won't let me comment on) installs the description even if the image is null.
>> 
>> I don't see it as a problem.
>> 
>> If the app developer wants to initialise the object with `null` image and a description, why don't we let them do it?
>> 
>> The image may be auto-generated and while it's generated, the image in `ImageIcon` remains `null`; when the image is read the app calls `ImageIcon.setImage` to the set now available image and *expects the description being preserved*.
>
>> Now .. what should happen to an existing description when you set a new image ?
>>
>> Apps can call setDescription() but in what order ?
>>
>> The only thing I can say here is that I think if the image is null we probably should clear the description.
> 
> Nothing should happen to the description. These are two fields which can be changed independently now, why would we enforce an order on method calls?
> 
> Changing the image shouldn't change the description; similarly, changing description doesn't change the image.

> > Also that other constructor (which github won't let me comment on) installs the description even if the image is null.
> 
> I don't see it as a problem.
> 
> If the app developer wants to initialise the object with `null` image and a description, why don't we let them do it?
> 

Because that  would be inconsistent with every other constructor.

See
ImagIcon(String filename)
ImageIcon(String filename, String description)
ImageIcon(String URL)
ImageIcon(String URL, String description)
ImageIcon(byte[] imageData)
ImageIcon(byte[] imageData, String description)

None of these set the description if the image resolves to null.

Also clearly
ImageIcon(Image image) 
and
ImageIcon(Image image, String description)
where image == null can't be doing it today
because a NPE is thrown before it gets there.
So we can have that case do whatever we want if we allow a null image.
And consistency seems the best choice.

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

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


More information about the client-libs-dev mailing list