looping Link items in a Umbraco.NestedContent causes NullReferenceException if the link's target page is deleted
Hi there!
I'm on Umbraco 8.5.3.
I have a composition called "Related Link List" for editors to add a list of links to a page. I used a Umbraco.NestedContent property and configured it with a "Link item" element that is an "Umbraco.MultiUrlPicker" configured to allow only 1 URL.
Normally this works great. I iterate the list and display the list o' links on a sidebar.
However, if the user makes a link to a page on the site and then deletes the page, I get a NullReferenceException that blows up the page.
I expect there is something in the Link construction that isn't checking to see if the target is trashed before trying to render.
What I'd like to do (I think) is test before trying to construct the link but I'm not sure how to do it; neither HasProperty or HasValue returns a false. Or maybe there's a better way? I can also wrap the thing in a try catch, but that seems not so elegant. :)
Is there a way to cast something so that I can test for isTrashed or something I can add to the linq query to only return valid links?
Yeah, if you get a Null reference exception in a razor file, look for the next razor usage of the object in question from the line you've been taken too. That's normally where the exception is actually been thrown tbh.
looping Link items in a Umbraco.NestedContent causes NullReferenceException if the link's target page is deleted
Hi there!
I'm on Umbraco 8.5.3.
I have a composition called "Related Link List" for editors to add a list of links to a page. I used a Umbraco.NestedContent property and configured it with a "Link item" element that is an "Umbraco.MultiUrlPicker" configured to allow only 1 URL.
Normally this works great. I iterate the list and display the list o' links on a sidebar.
However, if the user makes a link to a page on the site and then deletes the page, I get a NullReferenceException that blows up the page.
The error is thrown on this line:
I expect there is something in the Link construction that isn't checking to see if the target is trashed before trying to render.
What I'd like to do (I think) is test before trying to construct the link but I'm not sure how to do it; neither HasProperty or HasValue returns a false. Or maybe there's a better way? I can also wrap the thing in a try catch, but that seems not so elegant. :)
Is there a way to cast something so that I can test for isTrashed or something I can add to the linq query to only return valid links?
Thank you!
Hey Ken,
Razor often lies about which line the error is on.
Try this:
Thanks
Nik
I didn't realize that razor lies! :)
Yes, that totally did it. Thank you!
Yeah, if you get a Null reference exception in a razor file, look for the next razor usage of the object in question from the line you've been taken too. That's normally where the exception is actually been thrown tbh.
Nik
is working on a reply...