Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

DirectoryEntry NullReferenceException

Posted on 2006-07-17
8
301 Views
Last Modified: 2010-08-05
Hi, I need help with this error.  
I'm getting "Object referenced doesn't exist" error  when my codes reaches
   if (child.Properties["description"].Value.ToString().Length > 0)

Here the Child is a object of DirectoryEntry.  Thank you.


                                  case "CN=Groups"://Group
                                        if (child.Properties["description"].Value.ToString().Length > 0)
                                            NodeDetail.Items.Add(new ListViewItem(new string[] { split[1].ToString(), child.Properties["description"].Value.ToString() }, 4));
                                        else
                                            NodeDetail.Items.Add("Name", split[1].ToString(), 4);
                                        break;
0
Comment
Question by:lapucca
  • 3
  • 3
8 Comments
 
LVL 10

Accepted Solution

by:
athapa earned 500 total points
ID: 17127317
Check to see if child.Properties["description"].Value is null.

if (child.Properties["description"].Value != null && child.Properties["description"].Value.ToString().Length > 0)

child.Properties["description"] could be null too! So can child itself too.

AT
0
 

Author Comment

by:lapucca
ID: 17127849
I used the following and I got the same error.  I know as a fact that the "description" prrperty is null.  I want to code it so that it won't get error adding null to the listview.  Thanks. So if it's null then how should I code this?

child.Properties["description"].Value.ToString() != null
0
 

Expert Comment

by:martin75a
ID: 17128217
Add a little more checks in the if-clause:

if (
(child != null) &&
(child.Properties["description"] != null) &&
(child.Properties["description"].Value != null) &&
(child.Properties["description"].Value.ToString().Length > 0)
)
{
}
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:athapa
ID: 17129302
if you are certain that description is the only one which could be null then you could do this

if (child.Properties["description"] != null && child.Properties["description"] .Value.ToString().Lenght>0)
...

otherwise it is always safe to check them all as martin has shown.

AT
0
 

Author Comment

by:lapucca
ID: 17140872
Thanks everyone.  The child isn't null.  I was able to get the child.Name prioro to the the description code.  The NULL Oject Reference occur when it reached the code for "child.Properties["description"] ".  I tried both !=null and .length>0 and they both bomb out.  Also, this only happens when the child.Properties["description"] is null which is the same as .length=0.  

Thank you.
0
 
LVL 10

Expert Comment

by:athapa
ID: 17143445
if (child.Properties["description"] != null)  will ensure that the code which follows will only execute if description property is not null. .Length>0 won't work for null objects.

You could also use the try catch
 case "CN=Groups"://Group
try
{
       NodeDetail.Items.Add(new ListViewItem(new string[] { split[1].ToString(), child.Properties["description"].Value.ToString() }, 4));
   
}
catch (Exception ex)
{
   // do whatever needed if description is null
    NodeDetail.Items.Add("Name", split[1].ToString(), 4);

}
 break;

Checking null is better than try catch though.

AT
0
 

Author Comment

by:lapucca
ID: 17346795
Thanks AT.  I finally got it.  I'm getting the error becuase I was using the ToString when the child.Properties["description"].Value is null.  I just wasn't reading your answer carefully.  
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question