title: Why would .NET OpenXML SDK's SpreadsheetDocument.Open() method throw a NullReferenceException?
body: <p>I'm absolutely stumped by this, and could find nothing all morning searching Google.</p>
  
  <p>I have a method that looks more or less like this:</p>
  
  <pre><code>public void Open(string fileName, bool isEditable)
  {
      if(this.Document != null) this.Document.Close();
      this.Document = SpreadSheetDocument.Open(fileName, isEditable);
  }
  </code></pre>
  
  <p>It's only slightly more complex than that (for example, a provision to use a previously set filename if the arg is null or ZLS), but nothing relevant to the issue here.</p>
  
  <p>When I run this code I get a NullReferenceException on the last line of the method there.  Per the MSDN documentation, the only exceptions this method should throw are ArgumentNullException or OpenXMLPackageException.  The file is not open in any other application, I've tried moving it to various locations, in case it was a permissions issue, and it is most definitely a well-formed .xslx file, created and saved in Excel.</p>
  
  <p>In case it wasn't obvious, this is part of a helper-class I've written to reduce the workload involved in opening spreadsheets and extracting the data, mostly used for when it isn't as simple as importing into Access.  'this.Document' is a property of type SpreadsheetDocument.  Here's the exception detail:</p>
  
  <pre><code>System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=DocumentFormat.OpenXml
  StackTrace:
     at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.get_FileOpenAccess()
     at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.SavePartContents()
     at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose(Boolean disposing)
     at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose()
     at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Close()
     at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Load()
     at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.OpenCore(String path, Boolean readWriteMode)
     at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable, OpenSettings openSettings)
     at DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(String path, Boolean isEditable)
     at CorbLib.OXML.Spreadsheets.XLDoc.Open(String fileName, Boolean isEditable) in C:\[path removed]\XLDoc.cs:line 37
     at CorbLib.OXML.Spreadsheets.XLDoc.GetCell(String sheetName, String cellRef) in C:\[path removed]\XLDoc.cs:line 48
     at CorbLib.OXML.Spreadsheets.XLDoc.GetCellsFromRange(String range, Boolean leaveOpen) in C:\[path removed]\XLDoc.cs:line 139
     at CorbLib.OXML.Spreadsheets.XLDoc.GetCellValues(String range, Boolean leaveOpen) in C:\[path removed]\XLDoc.cs:line 116
     at ConsoleApplication1.Program.Main(String[] args) in C:\[path removed]\Program.cs:line 14
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
  </code></pre>
  
  <p>Any assistance wold be tremendously helpful.</p>
  
  <p><strong>EDIT:</strong> As per the comments in the accepted answer, I have modified the above method as follows...</p>
  
  <pre><code>public void Open(string fileName, bool isEditable)
  {
      /*unimportant bits*/
      var fs = new FileStream(this.FullPath, FileMode.Open, FileAccess.Read);
      this.Document = SpreadsheetDocument.Open(fs, this.IsEditable);
  }
  </code></pre>
  
  <p>...and it now works fine.</p>