[Bug SWING/22616] New: new ImageIcon().getIconHeight() throws NPE

timo dot lindfors at iki dot fi gcc-bugzilla@gcc.gnu.org
Fri Jul 22 19:11:00 GMT 2005


Steps to reproduce:
1. Compile and run the attached testcase.

Expected results:
1. Program prints out "-1".

Actual results:
1. Program throws the following exception:
java.lang.NullPointerException
   at javax.swing.ImageIcon.getIconHeight (ImageIcon.java:300)
   at testcase.<init> (testcase.java:9)
   at testcase.main (testcase.java:5)

Testcase:
import javax.swing.*;
import java.awt.*;
public class testcase {
        public static void main(String[] args) {
                new testcase();
        }
        public testcase() {
                ImageIcon icon = new ImageIcon();
                System.out.println(icon.getIconHeight());
        }
}

Note:
Severity is set to minor since error handling in Icon is probably undefined
behavior anyway since the javadoc does not say anything about what
getIconHeight() should return when no icon is loaded. Sun's JDK 1.5.0 returns -1
and so applications have started to depend on this undefined behavior.

Proposed fix:
Return -1 in getIconHeight() and getIconWidth() if no icon is loaded.

---
/home/lindi/cp-src/2005-07-22T100134+0000/classpath/javax/swing/ImageIcon.java.~1.16.~      2005-07-02 23:32:47.000000000 +0300
+++
/home/lindi/cp-src/2005-07-22T100134+0000/classpath/javax/swing/ImageIcon.java  
   2005-07-22 21:38:05.000000000 +0300
@@ -297,11 +297,15 @@

   public int getIconHeight()
   {
+    if (image == null)
+      return -1;
     return image.getHeight(observer);
   }

   public int getIconWidth()
   {
+    if (image == null)
+      return -1;
     return image.getWidth(observer);
   }

-- 
           Summary: new ImageIcon().getIconHeight() throws NPE
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: minor
          Priority: P2
         Component: SWING
        AssignedTo: graydon at redhat dot com
        ReportedBy: timo dot lindfors at iki dot fi
                CC: gcc-bugs at gcc dot gnu dot org,java-prs at gcc dot gnu
                    dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22616



More information about the Gcc-bugs mailing list