title: Sharepoint SPSecurity.RunWithElevatedPrivileges Object reference not set to an instance of an object
body: <p>I have a secure code block in SharePoint as following:</p>
  
  <pre><code>SPSecurity.RunWithElevatedPrivileges(delegate()
  {
      using (SPSite site = new SPSite(rootSite))
      {
          _web = site.OpenWeb(subSite);
      }
  });
  </code></pre>
  
  <p>Most of time, it works fine, but sometimes, I got this error:</p>
  
  <pre><code>System.NullReferenceException: Object reference not set to an instance of an object.
     at Microsoft.SharePoint.SPSecurity.&lt;&gt;c__DisplayClass4.&lt;RunWithElevatedPrivileges&gt;b__2()
     at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
     at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)
     at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
     at NCS.MDA.FiMS.CreateReturnFileTask.BatchJobManager..ctor()
  </code></pre>
  
  <p>Does anyone have any idea?
  Thanks</p>