1. public class Boxer1 {
2. Integer i;
3. int x;
4. public Boxer1(int y) {
5. x=i+y;
6. System.out.println(x);
7. }
8. public static void main(String[] args) {
9. new Boxer1(new Integer(4));
10. }
11. }
What is the result?
A. The value “4” is printed at the command line.
B. Compilation fails because of an error in line 5.
C. Compilation fails because of an error in line 9.
D. A NullPointerException occurs at runtime.
E. A NumberFormatException occurs at runtime.
F. An IllegalStateException occurs at runtime.
Answer: D
异常是不是new Integer(4)和 int y 不是一个类型的原因??一个是类而另一个是变量

解决方案 »

  1.   

    x = i + y;
    执行到这句的时候,i的值是null
    x被类初始化为0,而Integer的初始值是null
      

  2.   

    Integer I 取默认值为null;
        然而因为经过****constructor***初始化后在main中创建new S0034(new Integer(4));对象就为null;
        所以这一句就抛出NullPointerException了.
      

  3.   

    方法Boxer1(int y)要求传入的是一个int类型的值,是基本数据类型,可是你调用这个方法的时候传入的却是一个对象new Integer(4)
      

  4.   

    class Boxer {
      Integer i;
      int x=0;
      public Boxer(int y) {
        x=i+y;
        System.out.println(x);
       }
    }public class Boxer1{
      public static void main(String[] args) { 
         new Boxer(new Integer(4));  
      }
    }楼主的写法太乱了,我觉得这么写更能表达清楚,这里有2个问题:1.public Boxer(int y)构造函数的参数是int类型,而你传给个new Integer(4) 对象,是错误的。2.前面定义 Integer i; int x=0; 类型不一样,x=i+y;这步也会出错!错误代码:C:\J>java Boxer
    Exception in thread "main" java.lang.NoSuchMethodError: main