RelatedLinks.Any() ArgumentNullException: Value cannot be null
Hi all, many thanks for this.
I think I'm having trouble with my if statement.
I thought RelatedLinks.Any() should return a boolean, not null?
Everything displays fine if there is content in the notifications property, but if not, YSOD.
My code:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using Umbraco.Web.Models
@{
var home = Umbraco.TypedContent(1113);
var notifications = home.GetPropertyValue<RelatedLinks>("notifications");
}
@if(notifications.Any())
{
foreach(var notification in notifications)
{
<a href="@notification.Link">@notification.Caption</a>
}
} else {
<a>Nothing here</a>
}
If the RelatedLinks property editor is empty (offending line is #9):
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: source
Source Error:
Line 7: }
Line 8:
Line 9: @if(notifications.Any())
Line 10: {
Line 11: foreach(var notification in notifications)
Source File: c:\inetpub\wwwroot\mhshomes\Views\Partials\Notifications.cshtml Line: 9
The C# exception that is thrown when a null reference is passed to a method that does not accept it as a valid argument. An ArgumentNullException exception is thrown at run time in the following two major circumstances:
An uninstantiated object is passed to a method. To prevent the error,
instantiate the object.
An object returned from a method call is then passed as an argument
to a second method, but the value of the original returned object is
null. To prevent the error, check for a return value that is null and
call the second method only if the return value is not null.
RelatedLinks.Any() ArgumentNullException: Value cannot be null
Hi all, many thanks for this.
I think I'm having trouble with my if statement.
I thought RelatedLinks.Any() should return a boolean, not null?
Everything displays fine if there is content in the notifications property, but if not, YSOD.
My code:
If the RelatedLinks property editor is empty (offending line is #9):
Hi Paul
Use this code:
Thanks Alex, you're such a big help - I should have known that.
Marked as solution.
A suggestion: the documentation for Related Links property editors only shows:
If this 'null is not false' behaviour is inherit in C#, would that documentation be better off showing the full check including null:
But I'm new to this and there is probably a good reason.
https://our.umbraco.org/Documentation/Getting-Started/Backoffice/Property-Editors/Built-in-Property-Editors/Related-Links2
Many thanks again for your help.
The C# exception that is thrown when a null reference is passed to a method that does not accept it as a valid argument. An ArgumentNullException exception is thrown at run time in the following two major circumstances:
An uninstantiated object is passed to a method. To prevent the error, instantiate the object.
An object returned from a method call is then passed as an argument to a second method, but the value of the original returned object is null. To prevent the error, check for a return value that is null and call the second method only if the return value is not null.
is working on a reply...