Solved

DirectoryEntry NullReferenceException

Posted on 2006-07-17
8
299 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now