Accessing current node properties causes object exception
Hi There,
I'm using the function below to generate a list of nodes from my site. I'm wanting to access the properties of the of the document type in order to use more conditional logic but I'm getting the "Object reference not set to an instance of an object." . I have suspect bit of code marked in bold. Can someone help me fix this code please?
Thanks in advance.
Sean
public void iterateNodes(umbraco.presentation.nodeFactory.Node node) { if (node.Children.Count > 0) { var childNodes = node.Children; foreach (umbraco.presentation.nodeFactory.Node childNode in childNodes) { string strURL = umbraco.library.NiceUrl(childNode.Id);
You propably get the error because the umbracoNaviHide property doesn't exist on your child nodes. So before getting the value check that GetProperty doesn't return null
Accessing current node properties causes object exception
Hi There,
I'm using the function below to generate a list of nodes from my site. I'm wanting to access the properties of the of the document type in order to use more conditional logic but I'm getting the "Object reference not set to an instance of an object." . I have suspect bit of code marked in bold. Can someone help me fix this code please?
Thanks in advance.
Sean
public void iterateNodes(umbraco.presentation.nodeFactory.Node node)
{
if (node.Children.Count > 0)
{
var childNodes = node.Children;
foreach (umbraco.presentation.nodeFactory.Node childNode in childNodes)
{
string strURL = umbraco.library.NiceUrl(childNode.Id);
Response.Write(childNode.GetProperty("umbracoNaviHide").Value.ToString());
if (childNode.NodeTypeAlias == "HomepageType")
{
strb.AppendFormat("<li><a href=\"/\">Home</a></li>");
}
if (childNode.Children.Count > 0 && childNode.NodeTypeAlias != "HomepageType" && childNode.NodeTypeAlias == "SubPageType")
{
strb.AppendFormat("<li>");
strb.AppendFormat("<a href=\"{0}\">{1}</a>", strURL.ToString(), childNode.Name);
strb.AppendFormat("<ul>");
foreach (umbraco.presentation.nodeFactory.Node j in childNode.Children)
{
strb.AppendFormat("<li><a href=\"{0}\">{1}</a></li>", strURL.ToString(), j.Name);
}
strb.AppendFormat("</ul>");
strb.AppendFormat("</li>");
}
iterateNodes(childNode);
}
Literal1.Text = strb.ToString();
}
}
Hi Sean,
You propably get the error because the umbracoNaviHide property doesn't exist on your child nodes. So before getting the value check that GetProperty doesn't return null
If(childNode.GetProperty("umbracoNaviHide") != null){..........
Hope this helps you. Cheers,
Richard
Oh man. I can't believe I missed that. Thanks a bunch for the help.
is working on a reply...