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

Check for null object

P: n/a
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
Sep 19 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tarun Mistry" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
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

Sep 19 '06 #2

P: n/a
myClass test = new test();
test = null;

if(test == null)
{
System.Windows.Forms.MessageBox.Show("I am an idiot");
}

"Tarun Mistry" <no****@nospam.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
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

Sep 19 '06 #3

P: n/a
Tarun Mistry <no****@nospam.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 19 '06 #4

P: n/a
SP

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Tarun Mistry <no****@nospam.comwrote:
>what is the best/correct way to check for a NULL object?

Slight terminology clarification - there's no such thing as a null
object.
Slight terminology re-clarification: http://c2.com/cgi/wiki?NullObject

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 19 '06 #5

P: n/a
Daniel wrote:
myClass test = new test();
test = null;
myClass test = null;

Sorry, just being pedantic.

:)
if(test == null)
{
System.Windows.Forms.MessageBox.Show("I am an idiot");
Maybe you got this right though ;)

<...>

JB
Sep 20 '06 #6

P: n/a
SP wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
>Slight terminology clarification - there's no such thing as a null
object.

Slight terminology re-clarification: http://c2.com/cgi/wiki?NullObject
A NullObject has nothing to do with a null object, so
no need for a re-clarification.

Arne
Sep 20 '06 #7

P: n/a
PS

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:um1Qg.29$2g4.27@dukeread09...
SP wrote:
>"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft. com...
>>Slight terminology clarification - there's no such thing as a null
object.

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

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

Sep 20 '06 #8

P: n/a
Tarun,

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

private myClass test;

I hope this helps,

Cor

"Tarun Mistry" <no****@nospam.comschreef in bericht
news:%2****************@TK2MSFTNGP06.phx.gbl...
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

Sep 20 '06 #9

P: n/a
SP <ec***********@hotmail.comwrote:
Slight terminology clarification - there's no such thing as a null
object.

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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 20 '06 #10

P: n/a
PS <ec***********@hotmail.comwrote:
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.
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.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 20 '06 #11

P: n/a
"Tarun Mistry" <no****@nospam.comschrieb im Newsbeitrag
news:%2****************@TK2MSFTNGP06.phx.gbl...
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.
Thanks
Taz

Sep 20 '06 #12

P: n/a
PS

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
PS <ec***********@hotmail.comwrote:
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.

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
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 20 '06 #13

P: n/a
PS wrote:
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:um1Qg.29$2g4.27@dukeread09...
>SP wrote:
>>"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft .com...
Slight terminology clarification - there's no such thing as a null
object.

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

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.
This particlar subthread are discussing whether there is such a thing
as a null object.

A NullObject is not a null object.
Arne
Sep 21 '06 #14

P: n/a
PS

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:WclQg.83$2g4.24@dukeread09...
PS wrote:
>"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:um1Qg.29$2g4.27@dukeread09...
>>SP wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsof t.com...
Slight terminology clarification - there's no such thing as a null
object.

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

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.

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?

Sep 21 '06 #15

P: n/a
PS wrote:
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
Sep 21 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.