[英]null pointer exception. why?
I am a java newbie, so forgive me if this question may sound stupid.我是 Java 新手,如果这个问题听起来很愚蠢,请原谅我。 I am learning.
我在学习。
I am trying to calculate this sum but I am getting a strange error message.我正在尝试计算这个总和,但收到一条奇怪的错误消息。 Can you help me find me where it is?
你能帮我找到它在哪里吗? Thank you so much
非常感谢
public class myfirstjavaclass {
public static void main(String[] args) {
Integer myfirstinteger = new Integer(1);
Integer mysecondinteger = new Integer(2);
Integer mythirdinteger = null;
Integer result = myfirstinteger/mythirdinteger;
}
}
Exception in thread "main" java.lang.NullPointerException
at myfirstjavaclass.main(myfirstjavaclass.java:8)
You should not be using Integer
(the object type) here, as it can be null
(which you don't need and trip over here).您不应该在此处使用
Integer
(对象类型),因为它可以为null
(您不需要并在这里绊倒)。
When you dereference a null
in Java, you get a NullPointerException.当您在 Java 中取消引用
null
时,您会收到 NullPointerException。
In this case, it is a bit tricky, because automatic unboxing is involved (fancy name for conversion between the primitive types and their object wrappers).在这种情况下,它有点棘手,因为涉及自动拆箱(原始类型与其对象包装器之间转换的花哨名称)。 What happens under the hood is that
幕后发生的事情是
Integer result = myfirstinteger/mythirdinteger;
is really compiled as真的编译为
Integer result = Integer.valueOf(
myfirstinteger.intValue() / mythirdinteger.intValue());
The call to intValue()
fails on a null pointer.对
intValue()
的调用在空指针上失败。
Just use int
(the primitive).只需使用
int
(原语)。
public static void main(String[] args) {
int myfirstinteger = 1;
int mysecondinteger = 2;
int mythirdinteger = 0;
int result = myfirstinteger/mythirdinteger;
// will still fail, you cannot divide by 0
}
it seems to me your third Integer is assigned to null.在我看来,您的第三个整数被分配给了空值。
BTW, what do you really want to do? BTW,你真的想做什么? If you want to calculate a sum like you said in your question, see the code below
如果您想计算您在问题中所说的总和,请参阅下面的代码
public static void main(String[] args) {
int first = 1;
int second = 2;
int third = 0;
int sum = first + second + third;
}
If you want to calculate product, make sure you are not dividing by 0如果要计算乘积,请确保没有除以 0
public static void main(String[] args) {
int first = 1;
int second = 2;
int product = first / second; // this product is 0, because you are forcing an int
double product2 = (double) first / second; // this will be 0.5
}
"Null" means "this variable doesn't reference anything", which is another way of saying "this variable has no value". “空”的意思是“这个变量没有引用任何东西”,这是“这个变量没有值”的另一种说法。 It doesn't mean "the value is zero".
这并不意味着“价值为零”。
A NullPointerException is what Java gives you when you use a variable that doesn't reference anything, in a context that requires the variable to have a value. NullPointerException 是当您在需要变量具有值的上下文中使用不引用任何内容的变量时 Java 给您的异常。 Dividing a number by the value of a variable is a context which requires the variable to have a value - hence the exception.
一个数字除以一个变量的值是一个上下文,它要求变量具有一个值——因此是例外。
Because you divided by null
, of course.因为你除以
null
,当然。 What were you expecting to happen?你期待发生什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.