Copied to clipboard

Flag this post as spam?

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


  • Jesper Lysgaard 3 posts 83 karma points
    Mar 15, 2017 @ 09:50
    Jesper Lysgaard
    0

    Exception after upgrading to 7.5.11 from 7.4.3

    After running the 7.5.11 upgrade on our Umbraco web site I am getting an exception in one of my Partial View Macro Files (cshtml):

    System.NullReferenceException: Object reference not set to an instance of an object.
       at Umbraco.Web.PublishedContentExtensions.GetPropertyValue(IPublishedContent content, String alias)
    

    The exception is pointing to this line:

    var mediaItem = Umbraco.TypedMedia(item.GetPropertyValue("propAliasString"));
    

    The line is in this foreach loop:

    @foreach (var item in news.Where(x => x.GetPropertyValue<int>("group") == year.Key).OrderByDescending(y => y.GetPropertyValue("date")))
        {
            var mediaItem = Umbraco.TypedMedia(item.GetPropertyValue("announcement"));
    
                <div class="panel-body">
                    <span class="glyphicon glyphicon-file" aria-hidden="true"></span>&nbsp;<a href="@mediaItem.GetPropertyValue("umbracoFile")" target="_blank">@item.GetPropertyValue("headline")</a>
                    <p style="margin-left:16px;" class="date">@( Convert.ToDateTime(item.GetPropertyValue<string>("date")).Date.ToString("d. MMMMM yyyy"))</p>
                </div>
        }
    

    I have tried to browse the Umbraco documentation for changes in the api from 7.4 to 7.5 regarding this issue.

    Before the upgrade this worked.

    Has any of you experienced this kind of error? - and more important found a solution?

    best regards Jesper

  • Richard Eyres 98 posts 580 karma points
    Mar 15, 2017 @ 10:33
    Richard Eyres
    100

    I had a 'similar' issue in the past. Previously i was expecting a single result from a data type in an earlier version of umbraco, when i copied the code to a newer project, it failed. This was because the data type was not returning a collection instead of a single instance. I would check if that is the issue first. Secondly, i would do a little bit of defensive work - check if the item has the property and value for the item you want (announcement). Then check to see if it actually does return anything.

  • Jesper Lysgaard 3 posts 83 karma points
    Mar 15, 2017 @ 11:56
    Jesper Lysgaard
    0

    Hi Richard,

    Thanks. The issue was to check for value in the property - kind of embarrasing. But my excuse is that is used to work before the upgrade.

    var mediaItem = Umbraco.TypedMedia(item.GetPropertyValue("announcement"));
                        if(mediaItem != null)
                            {
                            <div class="panel-body">
                                <span class="glyphicon glyphicon-file" aria-hidden="true"></span>&nbsp;<a href="@mediaItem.GetPropertyValue("umbracoFile")" target="_blank">@item.GetPropertyValue("headdline")</a>
                                <p style="margin-left:16px;" class="date">@( Convert.ToDateTime(item.GetPropertyValue<string>("dato")).Date.ToString("d. MMMMM yyyy"))</p>
                            </div>
                            }
    

    /Jesper

  • Richard Eyres 98 posts 580 karma points
    Mar 15, 2017 @ 12:14
    Richard Eyres
    0

    Don't be embarrassed, as i only knew of the potential issue as it happened to me.

    Glad everything is now working for you.

  • Paul Wright (suedeapple) 277 posts 704 karma points
    Mar 15, 2017 @ 12:23
    Paul Wright (suedeapple)
    1
    Umbraco.TypedMedia(item.GetPropertyValue<int>("announcement",0));
    

    Will fallback to 0, if the item value is not a valid int, and this return NULL, which you check for later on in your code.

    ...also...

    @mediaItem.Url
    

    Saves you some typing :-)

    ... Also, maybe double check "headline"...

    @item.GetPropertyValue("headdline")
    
  • Jesper Lysgaard 3 posts 83 karma points
    Mar 15, 2017 @ 12:29
    Jesper Lysgaard
    0

    Hi Paul,

    That's a good trick. Thanks.

    /Jesper

Please Sign in or register to post replies

Write your reply to:

Draft