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

comparing an object with null

P: n/a
I have the following method in a class 'foo'.

public boolean equals(Object o) {
if (!(o.equals(null))) {
// do some stuff
}
}

The problem I am having is that, if the object that is passed in is
instanceof foo, then it recursively calls the equals method, and I get a
nullpointer exception. So how can I compare if the object that is being
passed in is null or not?

Thanks.
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Asad Khan" <uo**********@yahoo.com> wrote in message
news:zZ********************@news01.bloor.is.net.ca ble.rogers.com...
I have the following method in a class 'foo'.

public boolean equals(Object o) {
if (!(o.equals(null))) {
// do some stuff
}
}

The problem I am having is that, if the object that is passed in is
instanceof foo, then it recursively calls the equals method, and I get a
nullpointer exception. So how can I compare if the object that is being
passed in is null or not?


Remember, objects are not passed, object references are passed.

-- glen
Jul 17 '05 #2

P: n/a
"Asad Khan" <uo**********@yahoo.com> wrote in message news:<zZ********************@news01.bloor.is.net.c able.rogers.com>...
I have the following method in a class 'foo'.

public boolean equals(Object o) {
if (!(o.equals(null))) {
// do some stuff
}
}

The problem I am having is that, if the object that is passed in is
instanceof foo, then it recursively calls the equals method, and I get a
nullpointer exception. So how can I compare if the object that is being
passed in is null or not?

Thanks.

public boolean equals(Object o) {
if (!(o == null)){ //null is not an Object, that is 'none-object'
place holder refrence ... sole pointer in Java!
// do some stuff
}
else[
return false;
}
}
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.