1

In the Template Builder, when I run the Component Template with the Component I get the following error:

Debugging was started in process 'TcmTemplateDebugHost' with id 2688
Object reference not set to an instance of an object.
    at ABC.NavigationXML.CreateProductsSolutionsXML.buildNavigation(XElement xRoot, Component component)
    at ABC.NavigationXML.CreateProductsSolutionsXML.buildNavigation(XElement xRoot, Component component)
    at ABC.NavigationXML.CreateProductsSolutionsXML.Transform(Engine engine, Package package)
    at Tridion.ContentManager.Templating.Assembly.AssemblyMediator.Transform(Engine engine, Template template, Package package)
    at Tridion.ContentManager.Templating.Assembly.CSharpSourceCodeMediator.RunTemplate(Engine engine, Package package, String templateUri, String className)
    at Tridion.Templating.CSharpTemplate.CSharpSourceTemplate.Transform(Engine __engine, Package __package)
    at Tridion.ContentManager.Templating.Assembly.CSharpSourceCodeMediator.Transform(Engine engine, Template template, Package package)
    at Tridion.ContentManager.Templating.Engine.ExecuteTemplate(Template template, Package package)
    at Tridion.ContentManager.Templating.Engine.InvokeTemplate(Package package, TemplateInvocation templateInvocation, Template template)
    at Tridion.ContentManager.Templating.Compound.CompoundTemplateMediator.Transform(Engine engine, Template templateToTransform, Package package)
    at Tridion.ContentManager.Templating.Engine.ExecuteTemplate(Template template, Package package)
    at Tridion.ContentManager.Templating.Engine.InvokeTemplate(Package package, TemplateInvocation templateInvocation, Template template)
    at Tridion.ContentManager.Templating.Engine.TransformItem(Template template, IdentifiableObject itemToRender)
    at Tridion.ContentManager.Templating.Debugging.DebuggingEngine.Run()
    at Tridion.ContentManager.Templating.Debugging.DebugSession.Run()

I am not sure where to look for the error. There is a multivalued Component link field, the values of which if I remove the publishing works.

Update: This is how the buildnavigation function has been implemented

private void buildNavigation(XElement xRoot, Component component)
{
    Logger.Debug("begin" + component.get_Id());
    ItemFields fields = component.Fields();
    foreach (Component current in fields.Components("links"))
    {
        current.Fields();
        if (current.get_Schema().get_Title() == "dtd_item_list")
        {
            ItemFields fields2 = current.Fields();
            XElement xElement = new XElement("SiteMapNode");
            xElement.Add(new XAttribute("uri", current.get_Id()));
            xElement.Add(new XAttribute("title", this.GetTitle(fields2)));
            xRoot.Add(xElement);
            this.buildNavigation(xElement, current);
        }
        else
        {
            if (current.get_Schema().get_Title() == "dtd_link_item")
            {
                ItemFields fields3 = current.Fields();
                XElement xElement2 = new XElement("SiteMapNode");
                xElement2.Add(new XAttribute("uri", fields3.Component("linked_component").get_Id()));
                string text = (fields3.AsText("title") != string.Empty) ? fields3.AsText("title") : fields3.AsText("link_title");
                text = ((text != string.Empty) ? text : this.GetTitle(fields3));
                xElement2.Add(new XAttribute("title", text));
                xRoot.Add(xElement2);
            }
            else
            {
                ItemFields fields4 = current.Fields();
                XElement xElement3 = new XElement("SiteMapNode");
                xElement3.Add(new XAttribute("uri", current.get_Id()));
                string text2 = (fields4.AsText("title") != string.Empty) ? fields4.AsText("title") : fields4.AsText("link_title");
                text2 = ((text2 != string.Empty) ? text2 : this.GetTitle(fields4));
                xElement3.Add(new XAttribute("title", text2));
                xRoot.Add(xElement3);
            }
        }
    }
}
  • are you getting the same error when you publish or preview, or is the error only thrown in the Template Builder? – Bart Koopman Jun 5 '15 at 11:08
  • I am getting this error during publish also, and publish fails stating : "JScriptException: Expression 'RenderComponentPresentation()' had error 'Object reference not set to an instance of an object.'" – user1555 Jun 8 '15 at 6:04
  • I formatted your code to make it readable (please check how that is done, so you can do the same yourself next time). But looking at the answers from Peter and Bjørn, I think the problem might be more in the methods calling the buildNavigation method, since it sounds like either xRoot, and/or component might be null. Your best option seems to be in debugging the TBB, inspect which objects are null and why. – Bart Koopman Jun 8 '15 at 8:44
3

You should start in the CreateProductsSolutionsXML TBB (it's your own code). The buildNavigation method uses an object without first checking if the variable is null - and so you get this error.

To resolve it, the code should probably check for null and give a more specific error to guide users (if it requires user input of some kind).

| improve this answer | |
1

Canon.NavigationXML.CreateProductsSolutionsXML.buildNavigation(XElement xRoot, Component component) at Canon.NavigationXML.CreateProductsSolutionsXML.buildNavigation(XElement xRoot, Component component) at

Without the code it's only guesswork but it appears the TBB is recursively calling the buildNavigation method; most likely this method tries to call itself for each value in the component link field. One of the components in the 'tree' doesn't have any component links in this field (or even doesn't have the field if it was created with an older version of the schema) and the code doesn't handle this situation.

When parsing field values in code always guard against null values at every step:

//you did not check Component != null
ItemFields itemFields = new ItemFields(Component.Content, Component.Schema);
//you did not check itemFields.Contains("link")
Component linkedComponent = ((ComponentLinkField)itemFields["link"]).Value; 
//you did not check linkedComponent != null
System.Diagnostics.Trace.Write(linkedComponent.Id);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.