title: Nullable Property throwing NullReferenceException on .HasValue
body: <p>This line of (C#) code </p>
  
  <pre><code>if (!currentLap.S1.HasValue)
  </code></pre>
  
  <p>is giving me </p>
  
  <p>System.NullReferenceException: Object reference not set to an instance of an object.</p>
  
  <p>provided I'm sure that currentLap variable is instantiated (because it's being used a few lines before and it is a local variable) and it has following property:</p>
  
  <pre><code>private double? _s1;
  
  [DefaultValue(null)]
  [JsonConverter(typeof(ShortDoubleConverter))]
  public double? S1
  {
      get { return _s1; }
      set { _s1 = value; }
  }
  </code></pre>
  
  <p>how can it possibly throw NullReferenceException? Can it be something to do with optimization on Release mode?</p>
  
  <p>Thanks,
  Stevo</p>
  
  <p>EDIT: </p>
  
  <p>here is full method code. </p>
  
  <pre><code>public void Update(DriverData driverData)
      {
          LapInfo currentLap = this.CurrentLap;
  
          if (currentLap != null &amp;&amp;
         this.LastDriverData != null &amp;&amp;
         driverData.TotalLaps != this.LastDriverData.TotalLaps &amp;&amp;
         driverData.InPits &amp;&amp;
         driverData.Speed &lt; 10 &amp;&amp;
         !this.LastDriverData.InPits)
          {
              currentLap.Escaped = true;
          }    
  
          this.LastDriverData = driverData;
  
          if ((currentLap == null || currentLap.Lap != driverData.LapNumber) &amp;&amp;
              !this.Laps.TryGetValue(driverData.LapNumber, out currentLap))
          {
              currentLap = new LapInfo() { Lap = driverData.LapNumber, Parent = this, Class = driverData.Class };
              this.Laps.Add(driverData.LapNumber, currentLap);
  
              int lapsCount = 0, completedDriverLaps = 0, cleanLaps = 0;
              this.TotalLaps = driverData.TotalLaps;
  
              //if it's not the first lap
              if (driverData.TotalLaps &gt; 0)
              {
                  //previous lap
                  if (this.CurrentLap == null || !this.CurrentLap.Escaped)
                  {
                      this.CompletedLaps++;
  
                      if (this.CurrentLap == null || !this.CurrentLap.MaxIncident.HasValue)
                          this.CleanLaps++;
                  }
              }
  
              foreach (DriverLapsInfo laps in this.Parent.LapsByVehicle.Values)
              {
                  lapsCount += laps.TotalLaps;
                  completedDriverLaps += laps.CompletedLaps;
                  cleanLaps += laps.CleanLaps;
              }
  
              this.Parent.Parent.SetLapsCount(driverData, lapsCount, driverData.Class, completedDriverLaps, cleanLaps);
          }
  
          this.CurrentLap = currentLap;
  
          //add incidents
          if (driverData.Incidents != null)
          {
              foreach (IncidentScore incident in driverData.Incidents)
              {
                  this.CurrentLap.MaxIncident = Math.Max(this.CurrentLap.MaxIncident ?? 0, incident.Strength);
                  this.CurrentLap.Incidents++;
                  this.Incidents++;
              }                
          }
  
          LapInfo previousLap = null;
          if ((this.PreviousLap == null || this.PreviousLap.Lap != driverData.TotalLaps) &amp;&amp;
              this.Laps.TryGetValue(driverData.TotalLaps, out previousLap))
          {
              this.PreviousLap = previousLap;
  
              if (!this.PreviousLap.Date.HasValue)
              {
                  this.PreviousLap.Date = DateTime.UtcNow;
              }
          }
  
          if (currentLap.Position == 0)
              currentLap.Position = driverData.Position;
  
          if (driverData.CurrentS1 &gt; 0)
          {                
              **if (!currentLap.S1.HasValue)**
              {
                  this.UpdateBestS1(driverData.BestS1);
                  this.Parent.Parent.UpdateBestS1(driverData.BestS1, driverData.UniqueName);
                  currentLap.UpdateS1(driverData.CurrentS1, driverData);
  
                  //reset the best split set at the finish line
                  if (this.PreviousLap != null &amp;&amp; this.PreviousLap.SplitBest &lt; 0)
                      this.PreviousLap.SplitBest = 0;
              }
  
              if (driverData.CurrentS2.HasValue &amp;&amp; driverData.CurrentS1.HasValue &amp;&amp; !currentLap.S2.HasValue)
              {
                  double s2 = driverData.CurrentS2.Value - driverData.CurrentS1.Value;    
                  this.UpdateBestS2(s2);
                  this.Parent.Parent.UpdateBestS2(s2, driverData.UniqueName);
                  currentLap.UpdateS2(s2, driverData);                           
              }
          }            
  
          if (this.PreviousLap != null)
          {
              if (driverData.LastLap &gt; 0)
              {
                  if (!this.PreviousLap.S3.HasValue &amp;&amp; driverData.LastS2.HasValue)
                  {
                      double s3 = driverData.LastLap.Value - driverData.LastS2.Value;
                      this.UpdateBestS3(s3);
                      this.Parent.Parent.UpdateBestS3(s3, driverData.UniqueName);                        
                      this.PreviousLap.UpdateS3(s3, driverData);
                  }
  
                  if (!this.PreviousLap.LapTime.HasValue)
                  {
                      double? bestLap = this.Parent.Parent.BestLap;
                      this.PreviousLap.UpdateLapTime(driverData, 0);
                      this.Parent.Parent.UpdateBestLap(this.PreviousLap, driverData.BestLap, driverData);
                      this.UpdateBestLap(driverData.BestLap, this.PreviousLap);
                      this.PreviousLap.UpdateLapTime(driverData, bestLap);
                  }
              }
  
              else
              {
                  if (this.PreviousLap.SplitBest.HasValue)
                      this.PreviousLap.UpdateBestSplit();
                  if (this.PreviousLap.SplitSelf.HasValue)
                      this.PreviousLap.UpdateSelfSplit();
              }
          }
  
          if (driverData.InPits)
          {
              switch (driverData.Sector)
              {
                  case Sectors.Sector1:
                      if (previousLap != null)
                          previousLap.InPits = true;
                      break;
                  case Sectors.Sector3:
                      currentLap.InPits = true;
                      break;
              }
          }
  
          //lap to speed
          if (currentLap.TopSpeed &lt; driverData.Speed)
          {
              driverData.TopSpeedLap = driverData.Speed;
              currentLap.UpdateTopSpeed(driverData.Speed);
          }
          else
              driverData.TopSpeedLap = currentLap.TopSpeed;
  
          //overall top speed
          if (this.TopSpeed &lt; driverData.Speed)
          {
              driverData.TopSpeed = driverData.Speed;
              this.TopSpeed = driverData.Speed;
              this.Parent.Parent.UpdateTopSpeed(this.TopSpeed, driverData);
          }
  
          else
              driverData.TopSpeed = this.TopSpeed; 
  
      }
  </code></pre>
  
  <p>There is no way on earth the code can make it to that line and currentLap beeing null.</p>
  
  <p>Or am I going crazy? :)</p>