Developer forum

Forum » Templates » Newly created PropertyItem itemtype results in NullReferenceException when trying to do null check

Newly created PropertyItem itemtype results in NullReferenceException when trying to do null check

Jacob Storgaard Jensen
Reply

Hi,

I've just created an Item which I'm using for page properties.

When I in my layout template use the code below I get a NullReferenceException (System.NullReferenceException: Object reference not set to an instance of an object.)
But only on the pages that have not had their properties edited from backend... Isn't the PropertyItem attached to all pages when chosen on website settings?

I do check for null, but it's the var myField line it complains about (annonymized variable/field names ;-))...

@{
    string myValue = "";
    var myField = Model.PropertyItem.GetBoolean("MyCheckboxFieldName");
    if(myField != null) {
        myValue = !Model.PropertyItem.GetBoolean("MyCheckboxFieldName") ? "" : "the-css-class-i-use";
    }
}

Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

I think they aren't created until you save a page for the first time.

What about checking for null?

var myPropertyy = Model.PropertyItem;
if (myProperty != null) {
  var myField = Model.PropertyItem.GetBoolean("MyCheckboxFieldName");
  if(myField != null) {
  ...
}

 

Votes for this answer: 1
 
Jacob Storgaard Jensen
Reply

Hi Imar,

I just did that right before you wrote this, and it was returning null all the time... even after I (thought) I had edited/saved the page I was testing it on... but this old DW site was full of redirects so I was bloddy editing the wrong page :-D

Embarrassed I am!

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

;-)

 

You must be logged in to post in the forum