title: How to handle a null reference exception inside html helpers
body: <p>I am working on an asp.net mvc web application, and I have the following viewModel:-</p>
  
  <pre><code>public class ServerJoin
      {
          public TMSServer Server { get; set; }
          public Resource Resource { get; set; }
          public Technology Technology { get; set; }
          public ComponentDefinition ComponentDefinition { get; set; }
          public SDOrganization Site { get; set; }
          public SDOrganization Customer { get; set; }
          public IEnumerable&lt;NetworkInfo&gt; NetworkInfo { get; set; }
      }
  </code></pre>
  
  <p>On the _createANDupdate view that is used to  edit and create new ServerJoin object I have the following:-</p>
  
  <pre><code>@model TMS.ViewModels.ServerJoin
  
  &lt;span class="f"&gt;IP Address&lt;/span&gt; 
  
      @Html.EditorFor(model=&gt;model.NetworkInfo.IPADDRESS)
      @Html.ValidationMessageFor(model=&gt;model.NetworkInfo.IPADDRESS) | 
      @Html.CheckBox("IsIPUnique",new  { @checked = "checked" } ) IP Unique. 
  
  &lt;/div&gt;
  </code></pre>
  
  <p>But the problem I am facing is that the <code>NetworkInfo</code> is a collection so I am unable to directly access its <code>IPADDRESS</code> property. And in case I wrote the following foreach:-</p>
  
  <pre><code>@foreach(var n in Model.NetworkInfo)
  </code></pre>
  
  <p>I will get the null reference exception</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an objec</p>
  </blockquote>
  
  <p>So how I can solve this issue and have the following:-</p>
  
  <ol>
  <li><p>In case I am inside an edit view, I should display all IPaddress(1 editorfor, 2 editorfor, etc).</p></li>
  <li><p>In case I am inside the create view, I should create only one ip editorfor ?</p></li>
  </ol>
  
  <p>Thanks</p>