title: NullReferenceException on closing if brace
body: <p>I am using MVC with Razor views. In this particular view, I am passing a single instance of the class <code>Bed</code>. <code>Bed</code> has a property <code>string Infection</code>. Now in this instance, I have a boolean <code>HasInfection</code> defined in the view that I am using elsewhere to change what is displayed. This was originally declared as </p>
  
  <pre><code>var HasInfection = (Model.Infection.Trim() != "";
  </code></pre>
  
  <p>and worked as expected. However, there is now a use case where <code>Bed</code> may be null. Here is that first block of code:</p>
  
  <pre><code>@{
      ViewBag.Title = "Edit";
      var HasInfection = false;
      if (Model != null)
      {
          HasInfection = Model.Infection.Trim() != "";
      } // I get a NRE on this line whenever Model is null
  }
  </code></pre>
  
  <p>I have even tried the convoluted nested if-else solution, and I still get an NRE on the closing brace of <code>if</code>.</p>
  
  <pre><code>if (Model.Infection == null)
  {
      HasInfection = false;
  }
  else
  {
      if (Model.Infection != "")
      {
          HasInfection = true;
      }
      else
      {
          HasInfection = false;
      }
  }
  </code></pre>
  
  <p>I've tried every combination of &amp;/&amp;&amp;/|/|| I can think of with no success. If <code>Model</code> is <code>null</code> or <code>Model.Infection == ""</code>, <code>HasInfection</code> should be <code>false</code>.</p>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After attempting <code>var HasInfection = Model != null &amp;&amp; !string.IsNullOrWhiteSpace(Model.Infection);</code> (since <code>Infection</code> could be " "), I still get a NullReferenceException. Is it possible the issue is in the Controller even if the exception is in the View?</p>
  
  <pre><code>public ActionResult EditReservation(int Facility, string Room, string Bed)
  {
      var BedModel = New Bed();
      List&lt;Bed&gt; _b = BedModel.GetBed(Facility, Room, Bed);
      Bed result = _b.Where(bed =&gt; bed.BedStatus == "R" || bed.BedStatus == "A").FirstOrDefault();
      return View("Edit", result);
  }
  </code></pre>