将对象与 null 进行比较!

编程入门 行业动态 更新时间:2024-10-24 22:29:32
本文介绍了将对象与 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 进行比较!

本文发布于:2023-10-23 14:06:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521045.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!