Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you test for null elements in System.Array

I'm parsing an excel document into a System.Array and then using this data to develop a DataTable. The problem is that not every element in the System.Array contains a string and is null. See image below:

enter image description here

My question is how to I test for the null case.

Here is my method:

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;
    }

Here are some checks I've tried but didn't work

if (!values.Equals(null))
   dt.Rows[currentRow]["CVSS"] = values.GetValue(1, 3).ToString();
else
    dt.Rows[currentRow]["CVSS"] = "";

It appears this doesn't work because it seems like it is checking to see if any elements exist, not if the specfic element at this location is null.

if (values.GetValue(1, 3).ToString() == null)
    dt.Rows[currentRow]["CVSS"] = "";
else
   dt.Rows[currentRow]["CVSS"] = values.GetValue(1, 3).ToString();

This throws a NullReferenceExecption: Object reference not set to an instance of an object.

like image 963
Chris Avatar asked Aug 19 '26 03:08

Chris


2 Answers

  • The expression values.Equals(null) will throw on null, it won't return true.
  • Calling anything.ToString() will also throw on null, it won't return a null (or empty) string reference.

What you want to do is probably just:

if(values.GetValue(1, 3) == null) ...
like image 197
Theodoros Chatzigiannakis Avatar answered Aug 21 '26 16:08

Theodoros Chatzigiannakis


Change if (values.GetValue(1, 3).ToString() == null) to

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


Effectively your if, as you have written it could be "translated" to if(null.ToString() ...) in the case the value itself is null. And you can't call anything on null.
like image 29
KeyNone Avatar answered Aug 21 '26 16:08

KeyNone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!