将对象与 null 进行比较! [英] compare an object to null!
本文介绍了将对象与 null 进行比较!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试验证对象是否为空,并且我正在使用以下语法:
I am trying to verify whether an object is null or not and i am using this syntax:
void renderSearch(Customer c){
System.out.println("search customer rendering>...");
try {
if(!c.equals(null)){
System.out.println("search customer found...");
}else{
System.out.println("search customer not found...");
}
} catch (Exception e) {
System.err.println ("search customer rendering error: "
+ e.getMessage()+"-"+e.getClass());
}
}
我收到以下异常:
搜索客户呈现错误:空类 java.lang.NullPointerException
search customer rendering error: null class java.lang.NullPointerException
我以为我在用 if 和 else 循环考虑这种可能性.任何帮助将不胜感激.
I thought that I was considering this possibility with my if and else loop. Any help would be appreciated.
推荐答案
在 if 语句中尝试 c != null.您不是在比较对象本身,而是在比较它们的引用.
Try c != null in your if statement. You're not comparing the objects themselves, you're comparing their references.
这篇关于将对象与 null 进行比较!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文