Eclipse等于生成:null检查丢失? [英] Eclipse equals generation: null check missing?

查看:138
本文介绍了Eclipse等于生成:null检查丢失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让Eclipse为我的类生成了equals方法,它开始于:

  @Override 
public boolean equals(Object obj){
if(this == obj)
return true;
if(getClass()!= obj.getClass())
return false;
[...]

在我看来,像 if(obj == null)return false; 缺少。否则,如果null引用传递给equals,则在 obj.getClass()中将有一个空指针异常。我错了还是Eclipse错了?

解决方案

也许你有一个旧的eclipse版本。我的eclipse生成这个:

  if(this == obj)
return true;
if(obj == null)
return false;
if(getClass()!= obj.getClass())
return false;


I let Eclipse generate the equals method for my class and it starts with:

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (getClass() != obj.getClass())
        return false;
[...]

It seems to me, a check like if (obj == null) return false; is missing. Otherwise, if a null reference is passed to equals there will be a null pointer exception in obj.getClass(). Am I wrong or is Eclipse wrong?

解决方案

Perhaps you are having an old eclipse version. My eclipse generates this:

if (this == obj)
    return true;
if (obj == null)
    return false;
if (getClass() != obj.getClass())
    return false;

这篇关于Eclipse等于生成:null检查丢失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆