RFR: 8159055: ImageIcon.setImage can't handle null parameter [v15]
Phil Race
prr at openjdk.org
Tue Jul 22 18:23:02 UTC 2025
On Sun, 13 Jul 2025 09:12:27 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 two additional commits since the last revision:
>
> - Document NPE behaviour and update test and add class-level statement
> - Document NPE behaviour and update test and add class-level statement
src/java.desktop/share/classes/javax/swing/ImageIcon.java line 68:
> 66: * no exceptions will be thrown but the image will be 'effectively' null,
> 67: * as it will have no dimensions and never be drawn, and
> 68: * getImageLoadStatus() will report ERRORED.
ERRORED should be a link - I presume to MediaTracker.ERRORED
src/java.desktop/share/classes/javax/swing/ImageIcon.java line 215:
> 213: /**
> 214: * Creates an ImageIcon from the image.
> 215: * Passing {@code null} Image will result in {@code NullPointerException}.
"Passing" -> "Passing a"
src/java.desktop/share/classes/javax/swing/ImageIcon.java line 228:
> 226: * If the image has a "comment" property that is a string,
> 227: * then the string is used as the description of this icon.
> 228: * Passing {@code null} Image will result in {@code NullPointerException}.
"Passing" -> "Passing a"
src/java.desktop/share/classes/javax/swing/ImageIcon.java line 388:
> 386: public void setImage(Image image) {
> 387: this.image = image;
> 388: if (image == null) {
Umm. We had decided (in the end) to document the NPE.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/25767#discussion_r2223374128
PR Review Comment: https://git.openjdk.org/jdk/pull/25767#discussion_r2223376459
PR Review Comment: https://git.openjdk.org/jdk/pull/25767#discussion_r2223376633
PR Review Comment: https://git.openjdk.org/jdk/pull/25767#discussion_r2223418097
More information about the client-libs-dev
mailing list