?
Solved

DirectoryEntry NullReferenceException

Posted on 2006-07-17
8
Medium Priority
?
306 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
[X]
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
  • 3
  • 3
8 Comments
 
LVL 10

Accepted Solution

by:
athapa earned 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

650 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