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 183 karma points notactivated
    Jan 22, 2020 @ 07:26
    Kristian Overgaard

    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">
        @foreach (var blog in blogs)
            <div class="blog">

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

  • Nik 1299 posts 5501 karma points MVP 3x c-trib
    Jan 22, 2020 @ 09:20

    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 :-)


Please Sign in or register to post replies

Write your reply to: