0

文字列リストに項目を追加しています。挿入するデータが存在することはわかっていますが、null 参照エラー メッセージが表示され続けます。Object reference not set to an instance of an object.

    public class ExcelRow
        {
            public List<String> lstCells;
            public byte[] rowHash;
            ......
        }
    public class ExcelInfo
        {
            public List<ExcelRow> excelRows;
        }

    public ExcelInfo ReadExcel(OpenFileDialog openFileDialog)
        {
            var _excelFile = new ExcelQueryFactory(openFileDialog.FileName);
            var _info = from c in _excelFile.WorksheetNoHeader() select c;

            ExcelRow excelRow;
            ExcelInfo resp;

            resp = new ExcelInfo();

            foreach (var item in _info)
            {
                excelRow = new ExcelRow();
                excelRow.lstCells.Add(item.ElementAt(0));
                excelRow.lstCells.Add(item.ElementAt(1));
                excelRow.lstCells.Add(item.ElementAt(2));
                excelRow.lstCells.Add(item.ElementAt(3));
                .....
                excelRow.CalculateHash();
                resp.excelRows.Add(excelRow);
             }
            return resp;
       }

これは私がエラーを取得している場所です:excelRow.lstCells.Add(item.ElementAt(0));

4

2 に答える 2

2

コンストラクターで初期化する必要がありlstCellsます。ExcelRow

public class ExcelRow {
  public List<String> lstCells;
  public byte[] rowHash;
  public ExcelRow(){
     lstCells = new List<string>();
  }
  //....
}

public class ExcelInfo {
  public List<ExcelRow> excelRows = new List<ExcelRow>();
}
于 2013-08-27T01:48:21.913 に答える
1

コンストラクターを表示していませんExcelRowが、エラーから、インスタンス化lstCellsせず、null のままだと思います。

これは、プログラムをデバッグし、行内の各項目をマウスでポイントするか、行が再び機能し始めるまで一度に 1 つの項目を系統的に削除することで確認できます。そして問題の推測。

于 2013-08-27T01:49:16.400 に答える