Copied to clipboard

Flag this post as spam?

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


  • Kristian Overgaard 22 posts 185 karma points notactivated
    Jan 22, 2020 @ 07:26
    Kristian Overgaard
    0

    NullReferenceException on empty line in view

    I'm experiencing something pretty when testing a simple Umbraco page. I get a null reference exception, but according to the stacktrace, the line triggering the exception has nothing but a curly bracket.

    Here's the codeblock in question:

    @if (Model.BlogCollection != null) // confirmed that this is not null, if it's null it just skips the block correctly.
    { // line causing the exception according to stacktrace
    <div class="blogs-content">
        <div class="first-blog">
    
        </div>
        @foreach (var blog in blogs)
        {
            <div class="blog">
            </div>
        }
    </div>
    }
    

    Has anyone experienced something like this, and figured out what is causing it ?

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Jan 22, 2020 @ 09:20
    Nik
    100

    Hi Kristian,

    So this is the "downfall" of razor. There is infact code behind in a razor view, in that at runtime (unless configured otherwise) the view is compiled. As a result errors, particularly null references, can be reported on incorrect lines.

    The odds are, look for the next razor code in your file after the line being reported (e.g. something like @object.dosomething.this and you'll find you have your null reference there.

    Without seeing the exact razor block it's hard to give you a more accurate place to look :-)

    Nik

Please Sign in or register to post replies

Write your reply to:

Draft