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 不是一个类型的原因??一个是类而另一个是变量
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 不是一个类型的原因??一个是类而另一个是变量
解决方案 »
- 怎么编写程序来查U盘空间是否大于100M?
- java读取MF格式文件信息中文乱码,大侠们路过进来指点下迷津吧!
- java 很奇怪的一个数组声明int...x ,有谁用过?
- 有做过XSD校验XML文件的没,帮指点一下
- 想转行做Java,请大家帮帮我!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- Vector.get(int index)返回值的问题
- 手机程序开发中平台问题?不是难题,参与有分
- 如何配置jsp运行环境
- 谁手头上有CLDC1.0,mail一份给我,sun公司的网站我上不去,谢谢
- 问一个有关网络编程问题
- 请教jawin的参数问题
- 方法声明时,要声明这个方法抛出异常,有什么好处?高手进入!
执行到这句的时候,i的值是null
x被类初始化为0,而Integer的初始值是null
然而因为经过****constructor***初始化后在main中创建new S0034(new Integer(4));对象就为null;
所以这一句就抛出NullPointerException了.
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