469,360 Members | 1,600 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

comparing Object with null

public boolean equals(Object o) {
if (!(o.equals(null))) { // *
if (o instanceof foo) {
if (this.equals(o)) {
return true;
}
}
}
return false;
}

I have the above method in a class foo. When I run the following code:

f.equals(f)

where f is an object of class foo, then I get a nullpointerexception in this
method. The reason is that since f is of type foo, in line * it recursively
calls the equals method and then o becomes null, and of course I can't
dereference null.

So how can I check if an object is null or not?

Thanks.
Jul 17 '05 #1
3 16589
On Fri, 31 Oct 2003 16:20:48 GMT, "Asad Khan" <uo**********@yahoo.com>
wrote:
public boolean equals(Object o) {
if (!(o.equals(null))) { // *
if (o instanceof foo) {
if (this.equals(o)) {
return true;
}
}
}
return false;
}

I have the above method in a class foo. When I run the following code:

f.equals(f)

where f is an object of class foo, then I get a nullpointerexception in this
method. The reason is that since f is of type foo, in line * it recursively
calls the equals method and then o becomes null, and of course I can't
dereference null.

So how can I check if an object is null or not?


Did you try if (o != null)
Jul 17 '05 #2
Further to this answer different objects implement the method equals
differently. For example String objects implement it as a stright
comparrison of the contents of the string. So the equals method does not
tell you if one object is the same as another object. For this you need to
use the = = operator(without the space).
"Bryce (Work)" <sp******@berzerker-soft.com> wrote in message
news:ro********************************@4ax.com...
On Fri, 31 Oct 2003 16:20:48 GMT, "Asad Khan" <uo**********@yahoo.com>
wrote:
public boolean equals(Object o) {
if (!(o.equals(null))) { // *
if (o instanceof foo) {
if (this.equals(o)) {
return true;
}
}
}
return false;
}

I have the above method in a class foo. When I run the following code:

f.equals(f)

where f is an object of class foo, then I get a nullpointerexception in thismethod. The reason is that since f is of type foo, in line * it recursivelycalls the equals method and then o becomes null, and of course I can't
dereference null.

So how can I check if an object is null or not?


Did you try if (o != null)


Jul 17 '05 #3
You can collapse it to:
public boolean equals(Object o) {
if(!(o instanceof foo)) {return false;} //this catches the null case
too
foo f = (foo)o; //f will _never_ be null here
// else check for whatever attributes makes foos equal
// if you do this.equals(f) like in your original example it will be an
infinite loop
// and [probably] have a StackOverFlow exception
}
"Bryce (Work)" <sp******@berzerker-soft.com> wrote in message
news:ro********************************@4ax.com...
On Fri, 31 Oct 2003 16:20:48 GMT, "Asad Khan" <uo**********@yahoo.com>
wrote:
public boolean equals(Object o) {
if (!(o.equals(null))) { // *
if (o instanceof foo) {
if (this.equals(o)) {
return true;
}
}
}
return false;
}

I have the above method in a class foo. When I run the following code:

f.equals(f)

where f is an object of class foo, then I get a nullpointerexception in thismethod. The reason is that since f is of type foo, in line * it recursivelycalls the equals method and then o becomes null, and of course I can't
dereference null.

So how can I check if an object is null or not?


Did you try if (o != null)

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Asad Khan | last post: by
4 posts views Thread by - Steve - | last post: by
19 posts views Thread by Dennis | last post: by
5 posts views Thread by Kermit Piper | last post: by
19 posts views Thread by Ole Nielsby | last post: by
20 posts views Thread by Bill Pursell | last post: by
25 posts views Thread by J Caesar | last post: by
2 posts views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post: by
20 posts views Thread by Aaron Gray | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.