title: How did I get this NullReferenceException error here right after the constructor?
body: <p>I've had an asp.net website running live on our intranet for a couple of weeks now. I just got an email from my application_error emailer method with an unhandled exception. </p>
  
  <p>Here it is (I've cleaned up some of the paths to make it better displayed)</p>
  
  <blockquote>
    <p>Exception : Object reference not set to an instance of an object.
    Stack Trace : at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
     at TimesheetDomain.DataMappers.StaffMemberData.ReadStaff(SqlDataReader reader) in TimesheetDomain\DataMappers\StaffMemberData.cs:line 362</p>
    
    <p>at
    TimesheetDomain.DataMappers.StaffMemberData.GetStaffMember(String
    name) in
    TimesheetDomain\DataMappers\StaffMemberData.cs:line
    401</p>
    
    <p>at
    TimesheetDomain.ServiceLayer.TimesheetManager.GetUserFromName(String
    name) in
    TimesheetDomain\ServiceLayer\TimesheetManager.cs:line
    199 </p>
    
    <p>at UserVerification.GetCurrentUser()
    in \App_Code\UserVerification.cs:line
    29 at WebTimesheets.OnInit(EventArgs
    e) in
    \WebTimesheets\WebTimesheets.master.cs:line
    159</p>
    
    <p>at
    System.Web.UI.Control.InitRecursive(Control
    namingContainer) at
    System.Web.UI.Control.InitRecursive(Control
    namingContainer) at
    System.Web.UI.Page.ProcessRequestMain(Boolean
    includeStagesBeforeAsyncPoint, Boolean
    includeStagesAfterAsyncPoint)</p>
  </blockquote>
  
  <p>Basically it looks like it's erroring at my ReadStaff method which reads a data reader to build staff member objects. Here is the bit of code:</p>
  
  <pre><code>while (reader != null &amp;&amp; reader.Read())
  {
      StaffMember newMember = null;
      string firstName = reader["FirstName"].ToString();
      string lastName = reader["LastName"].ToString();
      int staffID = (int)reader["StaffID"];
      int employSection = (int)reader["EmploySection"];
      StaffType employType = (StaffType)employSection;
      string emailAddress = reader["EmailInt"].ToString();
      int employCode = (int)reader["ibbwid"];
  
      //check if they are an admin staff member 
      if (IsAdminStaff(employType))
      {
          newMember = new AdminOfficer(firstName, lastName, employType, staffID, emailAddress, employCode);
      }
      else
      {
          //check if they are a supervisor
          if (IsASupervisor(staffID))
              newMember = new Supervisor(firstName, lastName, employType, staffID, emailAddress, employCode);
          else
              newMember = new StaffMember(firstName, lastName, employType, staffID, emailAddress, employCode);
      }
  
      //add to identity map
      if (!_staffMembers.ContainsKey(staffID))
          _staffMembers.Add(staffID, newMember); //****THIS IS LINE 362*****
      else
          _staffMembers[staffID] = newMember;
  }
  </code></pre>
  
  <p>(Line 362 is 3rd last line)
  I'm using an identity map (just read fowlers book on patterns and thought it was a good idea - may have done it wrong, happy for comments) but that's not overly relevant as later on I use the <code>newMember</code> object elsewhere so if I remove that block the <code>NullReferenceException</code> will occur.</p>
  
  <p>I am struggling to see how on earth <code>newMember</code> is null in the 3rd last line there (which is the line that errored). </p>
  
  <p>Resharper/VS doesn't give me a warning that it could be <code>null</code> - because there's the 3 constructors which I choose from. </p>
  
  <p>Can anyone suggest where I can look to try and fix this error? It's only happened once and that method has been called thousands of times since the site went live. </p>
  
  <p>Thanks</p>
  
  <p>[EDIT]
  As Requested, here's the IComparer for staff member</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Comparer for staff members - compares on name
  /// &lt;/summary&gt;
  public class StaffMemberComparer : IComparer
  {
      public int Compare(object x, object y)
      {
          //check they are staff members
          if (x is StaffMember &amp;&amp; y is StaffMember)
          {
              //do a simple string comparison on names
              StaffMember staffX = x as StaffMember;
              StaffMember staffY = y as StaffMember;
  
              return String.Compare(staffX.FirstName, staffY.FirstName);
          }
  
          throw new Exception("This is for comparing Staff Members");
      }
  }
  </code></pre>
  
  <p>and it's used in the IComparable implementation</p>
  
  <pre><code>/// &lt;summary&gt;
  /// IComparable implementaiton
  /// &lt;/summary&gt;
  /// &lt;param name="obj"&gt;object to compare to&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  public int CompareTo(object obj)
  {
      StaffMemberComparer comparer = new StaffMemberComparer();
      return comparer.Compare(this, obj);
  }
  </code></pre>