6

I have a Razor view that starts like:

@using My.Models
@model MySpecificModel
@{
    ViewBag.Title = "My Title";  
    // NullReferenceException here:
    string dateUtc =  
        (Model == null ? DateTime.UtcNow.ToShortDateString() :
                         Model.Date.ToShortDateString());

I see no reason for the NullReferenceException in the last line (note: the " = ? :" stuff is on one line in my source code. It is formatted to fit here.)

I then remove the declaration of/assignment to dateUtc and the NullReferenceException moves up to the ViewBag.Title line:

@using My.Models
@model MySpecificModel
@{
    ViewBag.Title = "My Title";  // NullReferenceException here:

How can this possibly happen? ViewBag is of course not null.

NOTE 1: This only happens if Model is null.

NOTE 2: MySpecificModel.Date is of type DateTime, so can never be null.

4
  • It seems that razor doesn't really point the error accurately and it's possbile that the exception occurs later in the view. If it's not that, then just use the Null object pattern for the model. Commented Apr 29, 2013 at 6:44
  • @MikeSW: What is "the Null object pattern"? Commented Apr 29, 2013 at 6:46
  • You provide an empty default model which does nothing. It will be just so the Model isn't null. It will help to have an IsEmpty property . Better yet, if it can be applied in your case, a model with default values. The important thing is the Model not being null ever. Commented Apr 29, 2013 at 7:00
  • @MikeSW: That's exactly what I ended up doing. Feel free to add as an answer. Commented Apr 29, 2013 at 23:53

3 Answers 3

4

A NullReferenceException on ViewBag.Title can indicate the error is really on a nearby line. In this example the error was thrown on line 1 but the real error was the null Model.Invoice in line 3.

<h2>@ViewBag.Title</h2>
<div class="pull-right">
    <button onclick="addInvoice('@Model.Invoice.InvoiceId');">Add Invoice</button>
</div>

Also ASP.NET MVC Razor does not handle nulls in ternary statements like C# does.

//Ternaries can error in Razor if Model is null
string date = (Model == null ? DateTime.Now : Model.Date);

//Change to
string date = null;
if (Model == null)
    date = DateTime.Now;
else
    date = Model.Date;
Sign up to request clarification or add additional context in comments.

1 Comment

Same here.. ViewBag error was on line 6 ( @ViewBag.Title ). But the actual erroe was on line 2 ( ViewData["PageHeader"] ) Once I set the view data from the controller, the null reference exception was gone.
3

You provide an empty default model which does nothing. It will be just so the Model isn't null. It will help to have an IsEmpty property . Better yet, if it can be applied in your case, a model with default values. The important thing is the Model not being null ever

Comments

3

You will get a null exception if you do not pass any model to the view and still have the view bound to a model e.g.:

@model MySpecificModel 

When you did not pass such a model to the view from your controller.

2 Comments

That's a good thought, but I do pass null to the view as the model parameter.
Try removing ViewBag.Title = "My Title"; as well and see what happens :)

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.