Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Paul 18 posts 159 karma points
    Nov 21, 2017 @ 14:21
    Paul
    0

    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 
    
  • Alex Skrypnyk 5788 posts 22062 karma points MVP 4x admin c-trib
    Nov 21, 2017 @ 14:24
    Alex Skrypnyk
    100

    Hi Paul

    Use this code:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Umbraco.Web.Models
    
    @{
        var home = Umbraco.TypedContent(1113);
        var notifications = home.GetPropertyValue<RelatedLinks>("notifications");
    }
    
    @if (notifications != null && notifications.Any())
    {
        foreach (var notification in notifications)
        {
            <a href="@notification.Link">@notification.Caption</a>
        }
    }
    else
    {
        <a>Nothing here</a>
    }
    
  • Paul 18 posts 159 karma points
    Nov 22, 2017 @ 10:18
    Paul
    0

    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 (typedRelatedLinksConverted.Any())
    {
        // code here
    }
    

    If this 'null is not false' behaviour is inherit in C#, would that documentation be better off showing the full check including null:

    if (typedRelatedLinksConverted != null && typedRelatedLinksConverted.Any())
    {
        // code here
    }
    

    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.

  • malcomjarr 1 post 71 karma points
    1 week ago
    malcomjarr
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft