By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,779 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,779 IT Pros & Developers. It's quick & easy.

comparing Object with null

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.