C# 在引用前检查对象是否存在
C# Check if object exists before referencing
我想知道是否有一种方法可以在引用之前检查对象是否存在。不仅要检查它是否为空,因为它也不起作用。我正在使用 EPPlus 包读取 excel 文件,当它到达一个没有任何值数据的索引时,它会发送一个异常。
private DataTable WorksheetToDataTable(string tableName)
{
excelSheet = excelWorkbook.Worksheets[tableName];
DataTable dt = new DataTable();
try
{
int totalRows = excelSheet.Dimension.End.Row;
int totalCols = excelSheet.Dimension.End.Column;
for (int j = 1; j <= totalCols; j++)
{
dt.Columns.Add();
for (int i = 1; i <= totalRows; i++)
{
if (j == 1)
{
dt.Rows.Add();
}
try
{
dt.Rows[i - 1][j - 1] = excelSheet.Cells[i, j].Value.ToString();
//dt.Rows[i - 1][j - 1] = Object.ReferenceEquals(null, excelSheet.Cells[i, j].Value.ToString()) ? "" : excelSheet.Cells[i, j].Value.ToString();
}
catch
{
dt.Rows[i - 1][j - 1] = "";
}
}
}
return dt;
}
catch
{
MessageBox.Show("Error: Referenced Excel Table is empty, or indexed improperly! Check Excel formatting.", "Error");
return dt;
}
}
所以你可以看到我已经尝试检查 excelSheet.Cells[i, j].Value.ToString()
之前是否为 null 并且它发送了我在上面的 try/catch 中捕获的相同异常。我必须解析很多单元格,其中很多单元格将完全为空,这增加了很多
Exception thrown: 'System.NullReferenceException' in BBSApp.exe
到输出控制台。有没有办法在我调用对象之前检查对象是否存在,甚至在没有 try/catch 的情况下检查它是否为空?
if(excelSheet.Cells[i, j].Value.ToString() != null)
如上所示简单地检查它是否为 null 会发送相同的异常,因为它首先不存在。
试试这个:
if(excelSheet.Cells[i, j].Value != null)
您正在检查 Value
属性 是否为空。出现空引用异常是因为您正在尝试在 null
对象上调用方法 ToString()
。
或者,您可以使用空访问运算符:
if(excelSheet.Cells[i, j]?.Value?.ToString() != null)
在访问 excelSheet
字段之前,您可以使用空检查传播检查对象是否已初始化:
if (excelSheet.Cells[i, j]?.Value != null)
// Do whatever
根据the documentation,你应该使用IsEmpty
。
If the specified Range object is empty, returns the value Empty (use the IsEmpty function to test for this case). If the Range object contains more than one cell, returns an array of values (use the IsArray function to test for this case).
示例:
if (!excelSheet.Cells[i, j].Value.IsEmpty)
{
dt.Rows[i - 1][j - 1] = excelSheet.Cells[i, j].Value.ToString();
}
我想知道是否有一种方法可以在引用之前检查对象是否存在。不仅要检查它是否为空,因为它也不起作用。我正在使用 EPPlus 包读取 excel 文件,当它到达一个没有任何值数据的索引时,它会发送一个异常。
private DataTable WorksheetToDataTable(string tableName)
{
excelSheet = excelWorkbook.Worksheets[tableName];
DataTable dt = new DataTable();
try
{
int totalRows = excelSheet.Dimension.End.Row;
int totalCols = excelSheet.Dimension.End.Column;
for (int j = 1; j <= totalCols; j++)
{
dt.Columns.Add();
for (int i = 1; i <= totalRows; i++)
{
if (j == 1)
{
dt.Rows.Add();
}
try
{
dt.Rows[i - 1][j - 1] = excelSheet.Cells[i, j].Value.ToString();
//dt.Rows[i - 1][j - 1] = Object.ReferenceEquals(null, excelSheet.Cells[i, j].Value.ToString()) ? "" : excelSheet.Cells[i, j].Value.ToString();
}
catch
{
dt.Rows[i - 1][j - 1] = "";
}
}
}
return dt;
}
catch
{
MessageBox.Show("Error: Referenced Excel Table is empty, or indexed improperly! Check Excel formatting.", "Error");
return dt;
}
}
所以你可以看到我已经尝试检查 excelSheet.Cells[i, j].Value.ToString()
之前是否为 null 并且它发送了我在上面的 try/catch 中捕获的相同异常。我必须解析很多单元格,其中很多单元格将完全为空,这增加了很多
Exception thrown: 'System.NullReferenceException' in BBSApp.exe
到输出控制台。有没有办法在我调用对象之前检查对象是否存在,甚至在没有 try/catch 的情况下检查它是否为空?
if(excelSheet.Cells[i, j].Value.ToString() != null)
如上所示简单地检查它是否为 null 会发送相同的异常,因为它首先不存在。
试试这个:
if(excelSheet.Cells[i, j].Value != null)
您正在检查 Value
属性 是否为空。出现空引用异常是因为您正在尝试在 null
对象上调用方法 ToString()
。
或者,您可以使用空访问运算符:
if(excelSheet.Cells[i, j]?.Value?.ToString() != null)
在访问 excelSheet
字段之前,您可以使用空检查传播检查对象是否已初始化:
if (excelSheet.Cells[i, j]?.Value != null)
// Do whatever
根据the documentation,你应该使用IsEmpty
。
If the specified Range object is empty, returns the value Empty (use the IsEmpty function to test for this case). If the Range object contains more than one cell, returns an array of values (use the IsArray function to test for this case).
示例:
if (!excelSheet.Cells[i, j].Value.IsEmpty)
{
dt.Rows[i - 1][j - 1] = excelSheet.Cells[i, j].Value.ToString();
}