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;
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
lapuccaAuthor Commented:
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?
There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.
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
lapuccaAuthor Commented:
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.
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
lapuccaAuthor Commented:
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
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
if (child.Properties["descrip
child.Properties["descript
AT