0

I am having a small problem, regarding extracting data from an Excel file, ignore the first part where I am deleting some stuff, it is because the file being retrieved is 24 columns where the targeted one is around 12, so I am eliminating some columns.

Long story short, the part where I am trying to match if the value of column 5 in any row is "x" then write for example "E" in the second column of the same row ..else, check if the "X" is in the 6th column, then it means that "P" should be in the second column of that row and so on...

The problem is that for every cell it is either value "x" or it is empty ..(there is no value in the excel file), so there seems to be a problem whenever the code loops through empty elements, it throws this error

 l = new Microsoft.Office.Interop.Excel.Application();
 xl.Visible = false;
 Microsoft.Office.Interop.Excel.Workbook workbook = xl.Workbooks.Open(p_sUBKPath, Type.Missing, Type.Missing, 4, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

 Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];

 Microsoft.Office.Interop.Excel.Range range = ws.UsedRange;
 // delete columns that we don't need from the new excel file
 Microsoft.Office.Interop.Excel.Range range2 = ws.get_Range("A1","A1"); 
 range2.EntireColumn.Delete();
 Microsoft.Office.Interop.Excel.Range range3 = ws.get_Range("B1", "B1");
 range3.EntireColumn.Delete();
 Microsoft.Office.Interop.Excel.Range range4 = ws.get_Range("D1", "L1");
 range4.EntireColumn.Delete();
 Microsoft.Office.Interop.Excel.Range range5 = ws.get_Range("I1", "M1");
 range5.EntireColumn.Delete();
 Microsoft.Office.Interop.Excel.Range range6 = ws.get_Range("K1", "K1");
 range6.EntireColumn.Delete();

 //insert a new column ( Category)
 Microsoft.Office.Interop.Excel.Range range7 = ws.get_Range("B1", "B1");
 range7.EntireColumn.Insert(XlInsertShiftDirection.xlShiftToRight); 

 object[,] values = (object[,])range.Value2;
 values[1, 2] = (string)"Cat.";


 for (int row = 2; row <= values.GetUpperBound(0); row++)
 {
     try
     {
          if ((!String.IsNullOrEmpty(values[row, 5].ToString())) && values[row, 5].ToString() == "x")
          {
              values[row, 2] = (string)"E";
          }
          else if ((!String.IsNullOrEmpty(values[row, 6].ToString())) && values[row, 6].ToString() == "x")
          {
              values[row, 2] = (string)"P";
          }
          else if ((!String.IsNullOrEmpty(values[row, 7].ToString())) && values[row, 7].ToString() == "x")
          {
              values[row, 2] = (string)"Phy";
          }
          else if ((!String.IsNullOrEmpty(values[row, 8].ToString())) && values[row, 8].ToString() == "x")
          {
              values[row, 2] = (string)"L";
          }
          else if ((!String.IsNullOrEmpty(values[row, 9].ToString())) && values[row, 9].ToString() == "x")
          {
              values[row, 2] = (string)"Ex";
          }
          else
              MessageBox.Show("unknow");
       }
       catch (Exception ex)
       {
          MessageBox.Show(ex.ToString());
       }
}

The error is :

System.Nullreferenceexception object reference not set to an instance of an object

4

1 回答 1

1

我会在做 .tostring() 之前做一个空检查。IE:

(!String.IsNullOrEmpty(values[row, 5].ToString()))

!(values[row, 5] == null) && (!String.IsNullOrEmpty(values[row, 5].ToString()))
于 2013-09-20T15:08:49.947 回答