我正在尝试验证一个对象是否为 null,我正在使用以下语法:
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());
}
}
我得到以下异常:
search customer rendering error: null class java.lang.NullPointerException
我认为我正在用我的 if 和 else 循环考虑这种可能性。任何帮助将不胜感激。
最佳答案
在您的 if 语句中尝试 c != null 。您不是在比较对象本身,而是在比较它们的引用。
关于java - 将对象与 null 进行比较!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/994430/