generalcoder
generalcoder

Reputation: 2107

Cannot perform runtime binding on a null reference, But it is NOT a null reference

using: MVC 4, ASP.NET Razor

I'm getting an error that looks like it shouldn't be possible. It tells me that i'm using a null-reference, States, but clearly it is being set.

Controller:

public ActionResult Index()
{
    Dictionary<int, string> states = new Dictionary<int, string>()
    {
        { -1, "a"},
        { 0, "b"},
        { 1, "c"},
        { 2, "d"},
    };

    //assigning states
    ViewBag.States = states;

    foreach (KeyValuePair<int, string> de in ViewBag.States)
    {
        Debug.WriteLine(de.Key);
    }
    return View();
}

The View:

<div class="search-input">
    <select>
        @foreach (KeyValuePair<int, string> de in ViewBag.States)
        {
            <option value="@de.Key">@de.Value</option>
        }
    </select>
</div>

The error:

Cannot perform runtime binding on a null reference
Line 54: @foreach (KeyValuePair<int, string> de in ViewBag.States)

Upvotes: 91

Views: 209884

Answers (8)

Not related to Ravor, However in my case, I was getting this error constantly when running unit tests.

Thankfully, on further investigation, Was able to resolve this error - Actual error: cannot perform runtime binding on a null reference

Background: I was adding a new configuration file and wanted to write some tests for this. I've added this configuration file as well in the test project. For some reason, .NET Unit tests was not able to read this file during run time resolution (config file to my model class mapping). May be, the file was not available in the bin directory or the obj folders were not refreshed.

To solve this, I had to delete my bin & obj folders and rebuilt the project - which seems to be working.

.NET Unit tests and MSBuild were able to successfully fetch the file in the folder and was able to bind it to my model class & resolve (configuration file was mapped to my local model class)

Upvotes: 0

KEMBL
KEMBL

Reputation: 624

Not related to Razor, you'll see that exception in runtime trying access not existed property inside the dynamic value:

dynamic myValue;
if(myValue.notExistedProperty == "some-value") { ... }

Upvotes: 2

AFetter
AFetter

Reputation: 3584

This error happens when you have a ViewBag Non-Existent in your razor code calling a method.

Controller

public ActionResult Accept(int id)
{
    return View();
}

razor:

<div class="form-group">
      @Html.LabelFor(model => model.ToId, "To", htmlAttributes: new { @class = "control-label col-md-2" })
     <div class="col-md-10">
           @Html.Flag(Model.from)
     </div>
</div>
<div class="form-group">
     <div class="col-md-10">
          <input value="@ViewBag.MaximounAmount.ToString()" />@* HERE is the error *@ 
     </div>
</div>

For some reason, the .net aren't able to show the error in the correct line.

Normally this causes a lot of wasted time.

Upvotes: 20

Eonasdan
Eonasdan

Reputation: 7765

My solution to this error was that a copy and paste from another project that had a reference to @Model.Id. This particular page didn't have a model but the error line was so far off from the actual error I about never found it!

Upvotes: 5

XtraSimplicity
XtraSimplicity

Reputation: 6042

This exception is also thrown when a non-existent property is being updated dynamically, using reflection.

If one is using reflection to dynamically update property values, it's worth checking to make sure the passed PropertyName is identical to the actual property.

In my case, I was attempting to update Employee.firstName, but the property was actually Employee.FirstName.

Worth keeping in mind. :)

Upvotes: 18

Cuteboy_Max
Cuteboy_Max

Reputation: 131

You must define states not equal to null..

@if (ViewBag.States!= null)
{
    @foreach (KeyValuePair<int, string> de in ViewBag.States)
    {
        value="@de.Key">@de.Value 
    }
}                                

Upvotes: 1

generalcoder
generalcoder

Reputation: 2107

Found solution: I had typo in my view, ViewBag.Typo <-- this caused the error, but the debugger placed the exception at a irrelevant place.

Upvotes: 118

Alexandre
Alexandre

Reputation: 498

Set

 Dictionary<int, string> states = new Dictionary<int, string>()

as a property outside the function and inside the function insert the entries, it should work.

Upvotes: -4

Related Questions