Why is a "null reference" being reported when variable has a value?
<p>I have some C# code in my ASP.NET MVC3 project that's throwing an exception claiming:</p>

<pre><code>Cannot perform runtime binding on a null reference
</code></pre>

<p>The line of code throwing the exception is:</p>

<pre><code>ViewBag.Foo[i].Name = allSpark[i].Users.Name;
</code></pre>

<p>This is the code block:</p>

<pre><code>ViewBag.Foo = new myAllSparkModelType[allSpark.Length];
for (int i = 0; i &lt; allSpark.Length; i++)
{
    ViewBag.Foo[i].Name = allSpark[i].Users.Name;
    ...
}
</code></pre>

<p>When I set a breakpoint and inspect <code>allSpark[i].Users.Name</code>, it definitely has a value (e.g. "Fred").</p>

<p>If I comment out this line of code, the next line (which is similar code) then throws the same exception.</p>

<p>So the problem is either that <code>allSpark[i].Users.Name</code> is a null reference (Which I've confirmed isn't) or I can't just use the <code>ViewBag</code> like I am. If it's the latter, I'm confused as I thought that the ViewBag could be used this way.</p>
