473,513 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16927
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13319
by: Asad Khan | last post by:
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...
4
1327
by: - Steve - | last post by:
The following line of vb.net code works fine: if(strAnswer.toUpper() = strUserAnswer.toUpper()) However in an ASP.net page I'm told Object reference not set to an instance of an object ...
19
2620
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
5
3788
by: Kermit Piper | last post by:
Hello, I am comparing two date values, one from a database and one that has been converted from a hard-coded string into an actual Date type. So far so good. The problem I'm having is that one...
19
3808
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
20
2123
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
25
12989
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
2
1838
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi mister, I have an object with two properties, of type DateTime? (Nullable). Which is the best way for comparing ? The value of datetime can be null, and another value cannot be null, or two...
20
2255
by: Aaron Gray | last post by:
There does not seem too be anyway to test if two jQuery references are the same element. Given :- ... <div id="1"></div .... Then :- alert( $("#1") == $("#1"))
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7099
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.