Solved

DirectoryEntry NullReferenceException

Posted on 2006-07-17
8
297 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

13 Experts available now in Live!

Get 1:1 Help Now