我正在将整数对象与原始int进行比较,但是如果Integer对象是,则会得到空指针异常 null
public static void main(String[] args) {
Integer a = null;
int b = 1;
if (a != b) {
System.out.println("True");
}
}
Output : Exception in thread "main" java.lang.NullPointerException
at com.nfdil.loyalty.common.Test.main(Test.java:10)
Run Code Online (Sandbox Code Playgroud)
我得到这个是因为它试图转换null integer object (a.intValue()) into primitive int。
还有其他方法可以避免这种情况吗?
| 归档时间: |
|
| 查看次数: |
618 次 |
| 最近记录: |