Copied to clipboard

Flag this post as spam?

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


  • Alex Hedlund 6 posts 55 karma points
    Oct 28, 2014 @ 21:46
    Alex Hedlund
    0

    Null Reference Exception on simple implementation

    Hi there,

    I've been having issues with this plugin on several Umbraco 7.1.x installations(a 7.1.6 and a 7.1.8 I believe). The backoffice parts of the plugin work splendidly, but I get a NullReferenceException when I try to view a page. I've set a min & max items of 2 & 3 respectively on my data type. Here's a copy of my template (it's just a clone of the front page example)

    @using YouTube.Models
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{
        Layout = null;
    }

    @foreach (var typedVideo in Model.Content.GetPropertyValue("video"))
    {  
       
    @Html.Raw(typedVideo.Player.EmbedHtml)

        @typedVideo.Snippet.Title

        @typedVideo.Statistics.ViewCount

        @typedVideo.Snippet.PublishedAt.Value.ToShortDateString()
    }

    The error occurs on the @foreach line and prints the following stack trace.

    [NullReferenceException: Object reference not set to an instance of an object.]
       ASP._Page_Views_Sandbox2_cshtml.Execute() in c:\inetpub\wwwroot\Umbraco\Views\Sandbox2.cshtml:8
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +279
       System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +124
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +180
       Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) +139
       System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +379
       System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +32
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +613
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +613
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +263
       System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +240
       System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
       System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
       System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
       System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
       System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
       System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

    edit: I installed plugin version 1.0.2 build 65. Added new stack trace.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 28, 2014 @ 22:29
    Dennis Aaen
    0

    Hi Alex and welcome to our.

    I must say that I havenĀ“t try out this package myself, but I would try to help you the best I can.

    What I think you are missing is that you need to tell what type of data are gettig back, from the package page example I see that you are missing the <YouTube> after GetPropertyValue

    So try this:

    @using YouTube.Models
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    @{
        Layout = null;
    }

    @foreach (var typedVideo in Model.Content.GetPropertyValue<YouTube>("video")){  
       
    @Html.Raw(typedVideo.Player.EmbedHtml)

        @typedVideo.Snippet.Title

        @typedVideo.Statistics.ViewCount

        @typedVideo.Snippet.PublishedAt.Value.ToShortDateString()
    }

    Hope this helps,

    /Dennis

  • Alex Hedlund 6 posts 55 karma points
    Oct 28, 2014 @ 23:27
    Alex Hedlund
    0

    Thank you for the kind welcome and help!

    That's strange, I thought the return type was there. I fixed that but I'm still having the issue. Here's my new stack trace.

    [NullReferenceException: Object reference not set to an instance of an object.]
       ASP._Page_Views_YoutubeTester_cshtml.Execute() in c:\inetpub\wwwroot\Umbraco\Views\YoutubeTester.cshtml:7
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +279
       System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +124
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +180
       Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer) +139
       System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +379
       System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +32
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +613
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +613
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +263
       System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +240
       System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
       System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
       System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
       System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
       System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
       System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Oct 29, 2014 @ 13:50
    Dennis Aaen
    0

    Hi Alex,

    I have a look on your code once again. I think I spotted what give you the error.

    Try to change:

    @inheritsUmbraco.Web.Mvc.UmbracoTemplatePage

    To this line:

    @inherits UmbracoTemplatePage

    I have also add a check to check if the property has a value on the current page, if not, then it will not print out, this is a good thing to do if you have e.g a unorded list, or some other HTML around that you only want to print if the field contains a value.

    @inherits UmbracoTemplatePage
    @using YouTube.Models
      
    @{
        Layout = "umbLayout.cshtml"; 
    }
    @if(Model.Content.HasValue("video")){
      
        foreach (var typedVideo in Model.Content.GetPropertyValue("video")){ 
          
                @Html.Raw(typedVideo.Player.EmbedHtml)

                @typedVideo.Snippet.Title

                @typedVideo.Statistics.ViewCount

                @typedVideo.Snippet.PublishedAt.Value.ToShortDateString()
        }
      
    }

    Hope this helps,

    /Dennis

  • Alex Hedlund 6 posts 55 karma points
    Oct 29, 2014 @ 17:44
    Alex Hedlund
    100

    Hi Dennis,

    I figured out the problem using your advice. The string in the foreach statement needs to be the name of the document property (in my case "YouTube") not "video". I used the following template and kept changing things until I triggered the if statement (it also works with you inherits line change).

    @using YouTube.Models
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
    Layout = null;
    }

    <h2>Precondition test string</h2>
    @if(Model.Content.HasValue("YouTube")){
    <h2>If statement triggered</h2>
    }

    So now that I'm getting return values from Model, I should be able to run the foreach. I updated my old template, and I got a few videos to display on my page!

    @using YouTube.Models
    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
    Layout = null;
    }

    @foreach (var typedVideo in Model.Content.GetPropertyValue<YouTube>("YouTube"))
    {
    <div>@Html.Raw(typedVideo.Player.EmbedHtml)</div>
    @typedVideo.Snippet.Title<br/>
    @typedVideo.Statistics.ViewCount<br/>
    @typedVideo.Snippet.PublishedAt.Value.ToShortDateString()
    }

    Thank you so much for your help!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies