RFR: 8159055: ImageIcon.setImage can't handle null parameter [v14]
Alexey Ivanov
aivanov at openjdk.org
Tue Jul 8 16:43:45 UTC 2025
On Tue, 8 Jul 2025 06:22:28 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:
>
> constructor test removal
> > I think we need to have the "whole picture" tested to make sure everything does as we 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.
>
> I tested with null image/imagedata and invalid imagedata and it seems only these constructors throw NPE (current JDK) for null image (invalid image doesnt throw any exception)
I wrote a JUnit test, it's more convenient than a jtreg test:
<details>
<summary><code>ImageIconConstructorsTest.java</code></summary>
import java.awt.Image;
import java.net.URL;
import javax.swing.ImageIcon;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class ImageIconConstructorsTest {
@Test
public void noArgs() {
ImageIcon im = new ImageIcon();
assertNull(im.getImage());
assertNull(im.getDescription());
}
@Test
public void stringNull() {
ImageIcon im = new ImageIcon((String) null);
assertNotNull(im.getImage());
assertNull(im.getDescription());
}
@Test
public void stringNullNull() {
ImageIcon im = new ImageIcon((String) null, null);
assertNotNull(im.getImage());
assertNull(im.getDescription());
}
@Test
public void stringNullWithDescription() {
ImageIcon im = new ImageIcon((String) null, "Description");
assertNotNull(im.getImage());
assertEquals("Description", im.getDescription());
}
@Test
public void byteArrayNull() {
assertThrows(NullPointerException.class,
() -> new ImageIcon((byte[]) null));
// ImageIcon im = new ImageIcon((byte[]) null);
// assertNull(im.getImage());
// assertNull(im.getDescription());
}
@Test
public void byteArrayNullNull() {
assertThrows(NullPointerException.class,
() -> new ImageIcon((byte[]) null, null));
// ImageIcon im = new ImageIcon((byte[]) null, null);
// assertNull(im.getImage());
// assertNull(im.getDescription());
}
@Test
public void byteArrayNullWithDescription() {
assertThrows(NullPointerException.class,
() -> new ImageIcon((byte[]) null, "Description"));
// ImageIcon im = new ImageIcon((byte[]) null, "Description");
// assertNull(im.getImage());
// assertNull(im.getDescription());
}
@Test
public void urlNull() {
assertThrows(NullPointerException.class,
() -> new ImageIcon((URL) null));
// ImageIcon im = new ImageIcon((URL) null);
// assertNull(im.getImage());
// assertNull(im.getDescription());
}
@Test
public void urlNullNull() {
assertThrows(NullPointerException.class,
() -> new ImageIcon((URL) null, null));
// ImageIcon im = new ImageIcon((URL) null, null);
// assertNull(im.getImage());
// assertNull(im.getDescription());
}
@Test
public void urlNullWithDescription() {
assertThrows(NullPointerException.class,
() -> new ImageIcon((URL) null, "Description"));
// ImageIcon im = new ImageIcon((URL) null, "Description");
// assertNull(im.getImage());
// assertNull(im.getDescription());
}
@Test
public void imageNull() {
assertThrows(NullPointerException.class,
() -> new ImageIcon((Image) null));
// ImageIcon im = new ImageIcon((Image) null);
// assertNull(im.getImage());
// assertNull(im.getDescription());
}
@Test
public void imageNullNull() {
assertThrows(NullPointerException.class,
() -> new ImageIcon((Image) null, null));
// ImageIcon im = new ImageIcon((Image) null, null);
// assertNull(im.getImage());
// assertNull(im.getDescription());
}
@Test
public void imageNullWithDescription() {
assertThrows(NullPointerException.class,
() -> new ImageIcon((Image) null, "Description"));
// ImageIcon im = new ImageIcon((Image) null, "Description");
// assertNull(im.getImage());
// assertNull(im.getDescription());
}
}
</details>
All the test cases currently pass, and most constructors throw NPE for `null`. I haven't tested invalid arguments.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/25767#issuecomment-3049620519
More information about the client-libs-dev
mailing list