这个程序为什么会是NullPointerException?

来源:百度知道 编辑:UC知道 时间:2024/05/30 02:58:03
public class Boxer1
{
Integer i;
int x;
public Boxer1(int y)
{
x = i + y;
System.out.println(x);
}

public static void main(String[] args)
{
new Boxer1(new Integer(4));
}

}
@1楼 没错是JAVA的.SCJP试题来着.

Exception in thread "main" java.lang.NullPointerException
at exam.Boxer1.<init>(Boxer1.java:9)
at exam.Boxer1.main(Boxer1.java:15)

Integer i;
这个是对象,你声明了,但是并没有初始化。
x = i + y;
System.out.println(x);之前给它i初始化一下就好了。
例如:
i = new Integer(1);

java程序?