title: ASP.NET MVC 4 - Cannot perform runtime binding on a null reference
body: <p>I am trying to output a player's stats in a table. Not all players will have stats depending on the day. I have tried other ways and all are still complaining. Here is the code I have now:</p>
  
  <pre><code>      &lt;tbody&gt;
              @foreach(var player in @ViewBag.Roster){
                  int index = 0;
                  &lt;tr&gt;
                      &lt;td&gt;@player.Name, @player.TeamName @player.Position&lt;/td&gt;
                      if(@ViewBag.Stats[index] == null){
                          &lt;td&gt;--&lt;/td&gt;
                          &lt;td&gt;--&lt;/td&gt;
                          &lt;td&gt;--&lt;/td&gt;
                          &lt;td&gt;--&lt;/td&gt;
                      }
                      else{
                          &lt;td&gt;@ViewBag.Stats[index].Points&lt;/td&gt;
                          &lt;td&gt;@ViewBag.Stats[index].Rebounds&lt;/td&gt;
                          &lt;td&gt;@ViewBag.Stats[index].Assists&lt;/td&gt;
                          &lt;td&gt;@ViewBag.Stats[index].Turnovers&lt;/td&gt;                        
                      }
                  &lt;/tr&gt;
                  index++;
              }
  
          &lt;/tbody&gt;
  </code></pre>
  
  <p>Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference</p>
  
  <p>Source Error: </p>
  
  <p>Line 32:                         }</p>
  
  <p>Line 33:                         else{</p>
  
  <p>Line 34:                         @ViewBag.Stats[index].Points</p>
  
  <p>Line 35:                         @ViewBag.Stats[index].Rebounds</p>
  
  <p>Line 36:                         @ViewBag.Stats[index].Assists</p>