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

Phil Race prr at openjdk.org
Mon Jul 7 22:45:42 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

I think we need to have the "whole picture" tested to make sure everything does as we expect.
It isn't so easy to tell even from looking at the source of ImageIcon.
Then spec'ed so developers know what to expect. 
When I wrote a little test, I see null args do generate NPEs but invalid args don't result in null images
Toolkit images are installed, even if they can't be used because the source isn't valid.
They are "effectively" null, but not "actually" null.
We should at least specify the null arg behaviours and try to say something about  invalid image data.
And as a result, I am now flipping to just documenting the setImage(null) NPE
The protected method loadImage probably needs to say it too.
Invalid image data, maybe we can cover that in a class level statement.
Note: I'd like to see every constructor tested with both null and invalid image data.

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

PR Comment: https://git.openjdk.org/jdk/pull/25767#issuecomment-3046707358


More information about the client-libs-dev mailing list