我正在向字符串列表添加项目,我知道存在要插入的数据,但我不断收到空引用错误消息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));