Check for null object

  • Thread starter Thread starter Tarun Mistry
  • Start date Start date
T

Tarun Mistry

Hi guys,

what is the best/correct way to check for a NULL object?

I.e.

myClass test;

if(test == null) {}

This doesnt work.

Thanks
Taz
 
What do you mean it doesn't work? That is indeed the correct way to
check for null.

However, I suspect you are getting a compiler error telling you that the
test variable is not initialized. You might want to fix that first.

Hope this helps.
 
myClass test = new test();
test = null;

if(test == null)
{
System.Windows.Forms.MessageBox.Show("I am an idiot");
}
 
Tarun Mistry said:
what is the best/correct way to check for a NULL object?

Slight terminology clarification - there's no such thing as a null
object. A *reference* can be null, but an object can't.
I.e.

myClass test;

if(test == null) {}

This doesnt work.

That's precisely how you should check for nullity. Could you post a
short but complete program which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Arne Vajhøj said:
A NullObject has nothing to do with a null object, so
no need for a re-clarification.

They have a lot to do with each other. The Null Object pattern exists
because objects can be uninstansiated and your code can become cluttered
with lots of null checks. The OP should be made aware that there are
alternatives to == null.

PS
 
Tarun,

Set your "myClass test" global in your application and you will see that it
works.

private myClass test;

I hope this helps,

Cor
 
SP said:
Slight terminology re-clarification: http://c2.com/cgi/wiki?NullObject

Well, that's the "null object pattern" - it's certainly not what the OP
was talking about, and the object isn't *actually* null in the .NET
sense, it's just taking the role of an object which doesn't represent
anything. It's still a perfectly normal object as far as the language
and the runtime is concerned.
 
PS said:
They have a lot to do with each other. The Null Object pattern exists
because objects can be uninstansiated and your code can become cluttered
with lots of null checks. The OP should be made aware that there are
alternatives to == null.

Not if he wants to check whether a reference is null though. Given that
he's struggling with *that* part, isn't it worth getting that straight
before moving onto patterns to avoid the necessity of it in the first
place? (It's not like the Null Object Pattern is universally applicable
- the OP will certainly need to know about comparisons with null, and I
don't think confusing them with the NOP is a good idea at this point.)
 
Tarun Mistry said:
Hi guys,

what is the best/correct way to check for a NULL object?

I.e.

myClass test;

if(test == null) {}

This doesnt work.

This can fail if there is an overload defined for == in myClass.
If that's true in your case:
Check necessety and correct implementation of that overload
(as far as it is in your could.)
use:
if((object)test == null)
this will always check for reference equality.
 
Jon Skeet said:
Not if he wants to check whether a reference is null though. Given that
he's struggling with *that* part, isn't it worth getting that straight
before moving onto patterns to avoid the necessity of it in the first
place? (It's not like the Null Object Pattern is universally applicable
- the OP will certainly need to know about comparisons with null, and I
don't think confusing them with the NOP is a good idea at this point.)

I agree with you. It was hard to tell from the OP if the null test was to do
with simple object instansiation or to do with providing different behavior
based on whether the object was null which I generally treat as a code
smell. I don't like the idea of passing around an object reference that
equals null because of all the special handling that is required.

Regards,

PS
 
PS said:
They have a lot to do with each other. The Null Object pattern exists
because objects can be uninstansiated and your code can become cluttered
with lots of null checks. The OP should be made aware that there are
alternatives to == null.

This particlar subthread are discussing whether there is such a thing
as a null object.

A NullObject is not a null object.


Arne
 
Arne Vajhøj said:
This particlar subthread are discussing whether there is such a thing
as a null object.

A NullObject is not a null object.

Is a null object a NULL object?
 
PS said:
Is a null object a NULL object?

According to floating point NaN logic and
SQL NULL logic, then two non existing things
are not equal, so I would say: no.

Arne
 

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

Back
Top