473,549 Members | 2,745 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(nul l))) { // *
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 nullpointerexce ption 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 16931
On Fri, 31 Oct 2003 16:20:48 GMT, "Asad Khan" <uo**********@y ahoo.com>
wrote:
public boolean equals(Object o) {
if (!(o.equals(nul l))) { // *
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 nullpointerexce ption 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(withou t the space).
"Bryce (Work)" <sp******@berze rker-soft.com> wrote in message
news:ro******** *************** *********@4ax.c om...
On Fri, 31 Oct 2003 16:20:48 GMT, "Asad Khan" <uo**********@y ahoo.com>
wrote:
public boolean equals(Object o) {
if (!(o.equals(nul l))) { // *
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 nullpointerexce ption 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******@berze rker-soft.com> wrote in message
news:ro******** *************** *********@4ax.c om...
On Fri, 31 Oct 2003 16:20:48 GMT, "Asad Khan" <uo**********@y ahoo.com>
wrote:
public boolean equals(Object o) {
if (!(o.equals(nul l))) { // *
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 nullpointerexce ption 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
13322
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 in is instanceof foo, then it recursively calls the equals method, and I get a
4
1331
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 What am I doing wrong?
19
2628
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 = Color.Empty then..... or if mycolor is Color.Empty then .......
5
3794
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 of the values comes from the database, and for existing values it works fine, but if the date doesn't exist (which will always be the condition when...
19
3813
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 dictionary keys, based on their content, not their identity. Is this feasible using the arrays directly, or do I need to wrap them in a struct that...
20
2126
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 mmapped to a single page. I want to determine if the second is on the page. I'd like to do: #include...
25
12997
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 *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any...
2
1840
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 values are null, or two values aren't null. if (tarea.Fcprioridad < tarea.Fcentrega)
20
2264
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
7520
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7450
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7809
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5088
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1941
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 we have to send another system
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.