2017-02-15 39 views
0

我試圖運行一個腳本,使用EPPlus從Excel工作表讀取值並將它們加載到元組列表中。 然而,當我跑我得到兩個錯誤的劇本,第一個是:使用EPPlus腳本的TypeInitializationException

An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll 

我,我需要檢查內部異常其他職位都看到了,但是沒有一個是由Visual Studio 15 提供這是所有可用的例外細節。

System.TypeInitializationException was unhandled 
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll 
Additional information: The type initializer for 'CGCompare2.Program' threw an exception. 

然後當我關閉VS15例外窗口我看到一個彈出的對話:

Cannot access a disposed object. 
Object name: 'HwndSourceAdapter' 

我不能確定是什麼問題,如果這是我的代碼或不造成的。任何幫助,非常感謝。

Program.cs

using System; 
using System.Collections.Generic; 
using System.IO; 
using OfficeOpenXml; 

namespace CGComparer 
{ 
    class Program 
    { 
     private static List<Tuple<string, string>> _listTop; 
     private static List<Tuple<string, string>> _listGNED; 
     private static Base _baseCell; 
     private static ExcelPackage _package = new ExcelPackage(new FileInfo(_excelFile)); 
     private static string _excelFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), 
       "Compare GNED and TOP V1.0.xlsx"); 

     static void Main(string[] args) 
     { 
      _baseCell = new Base(1, 2); 
      _listTop = ColumnsToList(_baseCell.Column(),_baseCell.Row()); 

      _baseCell = new Base(3, 2); 
      _listGNED = ColumnsToList(_baseCell.Column(),_baseCell.Row()); 
     } 

     public static List<Tuple<string, string>> ColumnsToList(int column, int row) 
     { 
      var list = new List<Tuple<string, string>>(); 
      var ws = _package.Workbook.Worksheets[1]; 
      var ListIsValid = true; 
      do 
      { 
       var userEmail = (string)ws.Cells[column, row].Value; 
       var customerGroup = (string)ws.Cells[column + 1, row].Value; 

       if (!string.IsNullOrEmpty(userEmail)) 
       { 
        list.Add(new Tuple<string, string>(userEmail, customerGroup)); 
        row = row++; 
       } 
       else 
       { 
        ListIsValid = false; 
       } 

      } while (ListIsValid); 

      return list; 
     } 
    } 
} 

Base.cs

namespace CGComparer 
{ 
    public class Base 
    { 
     private static int _column; 
     private static int _row; 

     public Base(int column, int row) 
     { 
      _column = column; 
      _row = row; 
     } 

     public int Column() 
     { 
      return _column; 
     } 

     public int Row() 
     { 
      return _row; 
     } 
    } 
} 
+2

在VS2015的調試器是一個蹩腳的袋「O錯誤,它不會讓你看的InnerException。使用工具>選項>調試>常規>勾選‘使用管理兼容模式’,現在你可以看到仔細看看這些靜態圖,它們的初始值設定器可能會在後端糟糕地輸入字節數 –

+0

@HansPassant當我g et in。你能否詳細說明或發佈關於您提到的靜態警告的「答案」? –

+0

我不知道答案。我猜在NullReferenceException因爲_excelFile仍然是空的,你可以通過使用我推薦的調試器調整來得到一個事實。 –

回答

1

所以,原來的問題是盯着我的臉,我經歷了低於漢斯帕桑特的問題給予步驟:

「VS2015中的調試器是一個糟糕的包,它不會讓你看起來InnerException中的。使用工具>選項>調試>常規> 勾選「使用託管兼容模式」,現在您可以看到它。與靜態仔細 ,其初始值設定項字節你在後端 厲害。」

這是造成我宣佈了Excel與尚未被宣佈成爲路徑參數文件,一個空引用異常。

private static ExcelPackage _package = new ExcelPackage(new FileInfo(_excelFile)); 
private static string _excelFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), 
       "Compare GNED and TOP V1.0.xlsx");