473,382 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How does one check if an object's reference is NULL?

How does one check if an object's reference is null? I'd like to check
that rather than catch a nullreference exception.

thanks.

Sep 15 '06 #1
4 10955
if(obj == null)
{
// it's null
}
else
{
// it's not null
}

HTH

Simon.
"Karen Hill" <ka**********@yahoo.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
How does one check if an object's reference is null? I'd like to check
that rather than catch a nullreference exception.

thanks.

Sep 15 '06 #2
Karen,

If you have a variable, you can just use the equality operator, like so:

// The variable declaration.
MyClass instance = ...

To check to see if the variable is null, you do this:

if (instance == null)
{
// Code that executes when a variable is null is put here.
}

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

"Karen Hill" <ka**********@yahoo.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
How does one check if an object's reference is null? I'd like to check
that rather than catch a nullreference exception.

thanks.

Sep 15 '06 #3
Karen,

You're probably better off using ReferenceEquals(obj, null). This will
always work. The equality operator (==) could be overloaded and might
be checking if the values are equal instead of the references, so
ReferenceEquals() is a safer bet.

-Craig
Simon Tamman wrote:
if(obj == null)
{
// it's null
}
else
{
// it's not null
}

HTH

Simon.
"Karen Hill" <ka**********@yahoo.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
How does one check if an object's reference is null? I'd like to check
that rather than catch a nullreference exception.

thanks.
Sep 15 '06 #4
<cr**********@gmail.comwrote:
You're probably better off using ReferenceEquals(obj, null). This will
always work. The equality operator (==) could be overloaded and might
be checking if the values are equal instead of the references, so
ReferenceEquals() is a safer bet.
Any overload of == worth its salt should check for nullity anyway. I
believe that using == is more readable.

--
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 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: JGH | last post by:
How can I check if a key is defined in an associative array? var users = new array(); users = "Joe Blow"; users = "John Doe"; users = "Jane Doe"; function isUser (userID) { if (?????)
5
by: LL | last post by:
Hi, Where is the best place to check the session's object, if it's null, then redirect the user to login page? put into every page's load event? Thanks.
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
6
by: Bob Day | last post by:
VS 2003 The documentation says " Nothing keyword represents the default value of any data type" this is simply not true and causing a lot of problems. 1) Consider an SQL table of 3 columns: ...
48
by: avasilev | last post by:
Hi all, my question is: if i allocate some memory with malloc() and later free it (using free()), is there a possibility that a consequent malloc() will allocate memort at the same starting...
15
by: Tarun Mistry | last post by:
Hi guys, what is the best/correct way to check for a NULL object? I.e. myClass test; if(test == null) {}
30
by: Jess | last post by:
Hello, I tried a program as follows: include<iostream> using namespace std; class A{ public:
3
by: Yansky | last post by:
If I have the following code: var abc; if(!abc){ alert('test'); }
19
by: Michael C | last post by:
If we have something like this object x = null; MessageBox.Show(x.ToString()) we get an error. That kindof makes sense but there is no reason the behaviour couldn't be to return an empty...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.