Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在System.Array中测试空元素_C#_Arrays_System.array - Fatal编程技术网

C# 如何在System.Array中测试空元素

C# 如何在System.Array中测试空元素,c#,arrays,system.array,C#,Arrays,System.array,我正在将excel文档解析为System.Array,然后使用这些数据开发一个DataTable。问题在于并非System.Array中的每个元素都包含字符串且为空。见下图: 我的问题是如何测试空情况 以下是我的方法: public static System.Data.DataTable ConvertCSVtoDataTable(string strFilePath) { System.Data.DataTable dt = new System.Data.Data

我正在将excel文档解析为System.Array,然后使用这些数据开发一个DataTable。问题在于并非System.Array中的每个元素都包含字符串且为空。见下图:

我的问题是如何测试空情况

以下是我的方法:

public static System.Data.DataTable ConvertCSVtoDataTable(string strFilePath)
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        StreamReader sr = new StreamReader(strFilePath);
        string[] headers = sr.ReadLine().Split(',');
        foreach (string header in headers)
            dt.Columns.Add(header);
        sr.Close();

        Microsoft.Office.Interop.Excel.Workbook wb = null;
        Microsoft.Office.Interop.Excel.Worksheet ws = null;
        Microsoft.Office.Interop.Excel.Application app = null;
        int lastRow = 0;

        app = new Microsoft.Office.Interop.Excel.Application();
        app.Visible = false;
        wb = app.Workbooks.Open(strFilePath);
        ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1];
        lastRow = ws.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell).Row;
        int currentRow = 0;
        for (int index = 2; index <= lastRow; index++)
        {
            System.Array values = (System.Array)ws.get_Range("A" + index.ToString(), "M" + index.ToString()).Cells.Value;

                dt.Rows.Add();
            dt.Rows[currentRow]["Plugin ID"] = values.GetValue(1, 1).ToString();
            dt.Rows[currentRow]["CVE"] = values.GetValue(1, 2).ToString();
            dt.Rows[currentRow]["CVSS"] = values.GetValue(1, 3).ToString();
            dt.Rows[currentRow]["Risk"] = values.GetValue(1, 4).ToString();
            dt.Rows[currentRow]["Host"] = values.GetValue(1, 5).ToString();
            dt.Rows[currentRow]["Protocol"] = values.GetValue(1, 6).ToString();
            dt.Rows[currentRow]["Port"] = values.GetValue(1, 7).ToString();
            dt.Rows[currentRow]["Name"] = values.GetValue(1, 8).ToString();
            dt.Rows[currentRow]["Synopsis"] = values.GetValue(1, 9).ToString();
            dt.Rows[currentRow]["Description"] = values.GetValue(1, 10).ToString();
            dt.Rows[currentRow]["Solution"] = values.GetValue(1, 11).ToString();
            dt.Rows[currentRow]["See Also"] = values.GetValue(1, 12).ToString();
            dt.Rows[currentRow]["Plugin Output"] = values.GetValue(1, 13).ToString();
            currentRow++;
        }
        return dt;
    }
这似乎不起作用,因为它似乎是在检查是否存在任何元素,而不是检查此位置的specfic元素是否为null

if (values.GetValue(1, 3).ToString() == null)
    dt.Rows[currentRow]["CVSS"] = "";
else
   dt.Rows[currentRow]["CVSS"] = values.GetValue(1, 3).ToString();
这将抛出NullReferenceExection:
对象引用未设置为对象的实例。

  • 表达式
    values.Equals(null)
    将抛出
    null
    ,它不会返回
    true
  • 调用
    anything.ToString()
    也会抛出
    null
    ,它不会返回
    null
    (或空)字符串引用
您想要做的可能只是:

if(values.GetValue(1, 3) == null) ...
if(values.GetValue(1,3).ToString()==null)
更改为

if (values.GetValue(1, 3) == null)


实际上,您的
if
,如您所写,在值本身为null的情况下,它可以“转换”为
if(null.ToString()…)
。您不能在
null上调用任何东西

您不能在null上调用
ToString()
。您的if应该是:
if(values.GetValue(1,3)=null)
,因为
values
本身不是null,所以也可以使用
Convert.ToString(…)
。在这种情况下,如果
if (values.GetValue(1, 3) == null)