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

Prasanta Sadhukhan psadhukhan at openjdk.org
Fri Jun 20 05:06:35 UTC 2025


On Thu, 19 Jun 2025 17:48:08 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?
>>> 
>> 
>> 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.
>
>> And consistency seems the best choice.
> 
> Then our best choice is not to touch `ImageIcon(Image)` and leave it to throw `NullPointerException`.
> 
> All the constructors except for `ImageIcon((String) null)` throw `NullPointerException` if the first argument is `null`.
> 
> Strangely enough, `ImageIcon((String) null)` creates a non-`null` image.

If we leave it to throw NPE, then JCK might crib, if at some point they decide to add test to test null image and since javadoc doesn't mention it can throw NPE, it will result in non-compliance and then we have to modify javadoc to clarify it, so I guess it's best to take preventive action now..

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

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


More information about the client-libs-dev mailing list