Comparing Object with null

A

Asad Khan

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.
 
A

Adam

Asad Khan said:
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.

if (o != null) ....

or to check it is null:
o==null

Adam
 
X

xarax

Asad Khan said:
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.

// Your attempted implementation of equals(Object)
// is merely:
public boolean equals(Object obj)
{
return (this == obj);
}
// which is the default implementation in Object class.


If you have a special case of equals that compares
other stuff, then you probably want something like:

public boolean equalsFoo(Foo foo)
{
// perform special case testing here.
return (this == foo); // whatever else you want to test
}

public boolean equals(Object obj)
{
return (null != obj)
&& ((this == obj)
|| ((obj instanceof Foo)
&& equalsFoo((Foo) obj)));
}

HTH
 
R

Roedy Green

if (!(o.equals(null))) { // *
if (o instanceof foo) {
if (this.equals(o)) {
return true;

the first two lines are unnecessary. You can shorten that to

return this.equals( o );

But that is equivalent to having no method at all, just using the
default equals method.

Fresh Fish For Sale

Obviously, they are for sale, why put up a sign otherwise

Fresh Fish

Obviously they are fresh. Otherwise you could smell them.

Fish

Obviously they are fish. Any fool can see that.
 
G

Gary Labowitz

Asad Khan said:
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?

I think you don't mean to "check if an object is null" or else I don't
understand what you mean. If you want to see if the reference variable does
not have a reference to an object, that would be "check if the reference is
null."
And you do that with
f == null
This confusion comes about from saying things like: "where f is an object of
class foo." While that is commonly said, it really is "where f is a
reference to an object of class foo."
Say the wrong thing, but think the right thing.
 
G

Guest

If you have a special case of equals that compares other stuff, then you
probably want something like:

public boolean equalsFoo(Foo foo)
{
// perform special case testing here.
return (this == foo);
//whatever else you want to test
}

public boolean equals(Object obj)
{
return (null != obj)
&& ((this == obj)
|| ((obj instanceof Foo)
&& equalsFoo((Foo) obj)));
}

HTH

hashCode() and equals() work in tandem, so if you override one, so should
override the other. In particular, equals() should check any state used
in computing the hashCode().

-- La'ie Techie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top