Solved

DirectoryEntry NullReferenceException

Posted on 2006-07-17
8
302 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 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
Technology Partners: 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

733 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