4

I have an MVC view that is throwing a NullReferenceException in the following code:

<div class="center-block text-center mb15">
    Showing @count of @Model.TotalItemCount results
</div>

So, I would expect that the error is that Model is null. However, the code has already made it through the following code which appears earlier in the same view:

@{
    long count = Model.Count();
}

Stepping through the code shows that count is (in this case) 6, and Model.TotalItemCount is also 6, so there is no null reference.

But it's still throwing the exception!

Here's the weird bit, which makes no sense to me and is the crux of the question. When the exception is thrown I get the following in Visual Studio:

enter image description here

This seems to indicate that the view that is being debugged is different to the view being displayed in the editor, but

  • I have deleted the bin and obj folders and rebuilt the app, with no change, and
  • This view has not been changed for a couple of months and works with other controller actions (it is a Shared view).

Stepping through the view prior to this line in the debugger, every line is highlighted correctly (yellow bar through all text).

5
  • Does doing Showing @Model.Count() of @Model.TotalItemCount results also throw the error? Commented Apr 13, 2017 at 20:00
  • 2
    Is TotalItemCount an auto-property? Or does it have an implementation? What is the implementation of Count? I assume it's just IEnumerable.Count() but I'd like to confirm. Commented Apr 13, 2017 at 20:02
  • 1
    One more thing, I have noted in the past that the debugger sometimes misidentifies the line in Razor files that throws the actual error. Can you share the entire view with us, snipping out the irrelevant HTML? Commented Apr 13, 2017 at 20:04
  • I notice you are checking Model.Count and not Model.Count() in the if, so not sure how the view compiles! Commented Apr 13, 2017 at 20:15
  • Model is an instance of Troy Goode's IPagedList<T>, and implements a Count property as well as inheriting from IEnumerable<T>. Commented Apr 13, 2017 at 20:37

1 Answer 1

6

The answer was to look a little wider. Although the error was marked as shown in Visual Studio, and the exception indicated that it was thrown by the indicated line number, the exception was actually due to null reference about 5 lines later (the intervening lines are all HTML) where a property was accessed for a value that was, in fact, null.

Unfortunately Razor view debugging has not achieved the maturity it should have after this long. Thanks for the input to all who commented.

Sign up to request clarification or add additional context in comments.

1 Comment

Usually it means there are stale files being used, and usually restarting the IIS Express and/or VS and/or cleaning the solution helps. Otherwise debugging razor views works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.