Creating new property on a document type causes 'Null Reference' exception
Hello,
I am fairly new to Umbraco and would like some help with a question that I am facing.
Once I create a document type and an associated template in Umbraco, the content pages that are mapped to this doc type/template work fine. However, if I add a property on the document type and then code the required bits in the template to display the value held by the new property, I find that if I do not validate for a null entry before accessing the value of the property, pages which do not have a value for the new property throw a 'Null Reference' expection. Could you tell me why this is and if there is a more elegant way to tackle this issue.
This is because the published cache for the existing documents do not yet have these properties on them. You can either check for a null reference (always a good practice) or remember to publish all of the documents of this type.
Note: Right-clicking "Content" and choosing "Republish all" does not work, you have to actually publish all of the nodes affected, if they are under a single parent, just right-click the parent, choose "Publish" and check on "Publish this page and all of it's children".
I think the issue is exactly what you say. You're accessing the value on an item that is still null. Passing a null value into GetMedia() or NiceUrl() will throw an exception.
You should always code to check if a field value is null before using it. This IS the most elegant way to do things.
Creating new property on a document type causes 'Null Reference' exception
Hello,
I am fairly new to Umbraco and would like some help with a question that I am facing.
Once I create a document type and an associated template in Umbraco, the content pages that are mapped to this doc type/template work fine. However, if I add a property on the document type and then code the required bits in the template to display the value held by the new property, I find that if I do not validate for a null entry before accessing the value of the property, pages which do not have a value for the new property throw a 'Null Reference' expection. Could you tell me why this is and if there is a more elegant way to tackle this issue.
Thanks,
Shwetha
This is because the published cache for the existing documents do not yet have these properties on them. You can either check for a null reference (always a good practice) or remember to publish all of the documents of this type.
Note: Right-clicking "Content" and choosing "Republish all" does not work, you have to actually publish all of the nodes affected, if they are under a single parent, just right-click the parent, choose "Publish" and check on "Publish this page and all of it's children".
It all depends on the type of property. What is the datatype?
Thanks for the responses. The datatype is a Textstring.
I think the issue is exactly what you say. You're accessing the value on an item that is still null. Passing a null value into GetMedia() or NiceUrl() will throw an exception.
You should always code to check if a field value is null before using it. This IS the most elegant way to do things.
is working on a reply...