RFR: 8159055: ImageIcon.setImage and ImageIcon(Image) constructor can't handle null parameter [v12]
Prasanta Sadhukhan
psadhukhan at openjdk.org
Thu Jul 3 06:48:45 UTC 2025
On Thu, 3 Jul 2025 05:25:59 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:
>
> javadoc change, description setting removed
> Both image and description are two independent fields of ImageIcon object, each has its own getter and setter, and each can be changed independently. We should not enforce the order of calls: if an app developer wants to set the image to null, temporarily or not, the value of the description has to be preserved
I guess resetting the description came from the fact that description is mentioned as "Sets the description of the image. This is meant to be a brief textual description of the object." so if image is null, the description of the image logically should be nothing.
But again, as pointed out, description has its own setter and getter so user would expect whatever description is stored either by [ImageIcon](https://docs.oracle.com/en/java/javase/24/docs/api/java.desktop/javax/swing/ImageIcon.html#%3Cinit%3E(java.awt.Image,java.lang.String))([Image](https://docs.oracle.com/en/java/javase/24/docs/api/java.desktop/java/awt/Image.html) image, [String](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/String.html) description) constructor or `setDescription `would be returned same via `getDescription`..
If not, the spec for set and getDescription needs to be modified to to incorporate null-image scenario.
But all-in-all I guess this description change can go in separate PR along with [above change](https://github.com/openjdk/jdk/pull/25767#discussion_r2164102520)
-------------
PR Comment: https://git.openjdk.org/jdk/pull/25767#issuecomment-3031070723
More information about the client-libs-dev
mailing list