无法对空引用异常执行运行时绑定

时间:2021-02-18 04:15:25

I'm trying to find the dimensions of an Excel table using C# by finding the first null cell within the first column (which consists of dates) and the header row.

我试图通过查找第一列(由日期组成)和标题行中的第一个空单元格来使用C#查找Excel表的维度。

Here's the code I'm using right now:

这是我现在正在使用的代码:

public static void findingTableBounds()
    {
        string dateCol = "";
        ArrayList dateColumn = new ArrayList();
        ArrayList numberOfColumns = new ArrayList();

        for (int column = 1; column < currentRow; column++)
        {
            dateCol = ((Excel.Range)workSheet.Cells[currentRow, 1]).Value2.ToString();
            if (dateCol != "")
            {
                dateColumn.Add(dateCol);
                currentRow++;
                totalRow++;
                Console.WriteLine("Total Row: {0}", totalRow);            
            }
            else
            {
                Console.WriteLine("Total Row: {0}", totalRow);
                currentRow = 2;
            }
        }

**Note: There is a closing bracket for this method, I didn't include it because there's another for loop that does the exact same thing as the above code but only for how many columns there are.

**注意:这个方法有一个结束括号,我没有包含它,因为有另一个for循环与上面的代码完全相同但只有多少列。

The error occurs at "dateCol = ((Excel.Range)workSheet.Cells[currentRow, 1]).Value2.ToString();" I'm pretty sure it happens because I'm trying to assign a null value (the cell) to dateCol (a string) when string is a non-nullable type. Unfortunately I'm not sure how to solve the problem.

错误发生在“dateCol =((Excel.Range)workSheet.Cells [currentRow,1])。Value2.ToString();”我很确定它会发生,因为我正在尝试在string为非可空类型时将null值(单元格)分配给dateCol(字符串)。不幸的是我不知道如何解决这个问题。

2 个解决方案

#1


9  

Applying a null value to a string is doable, but if ((Excel.Range)workSheet.Cells[currentRow, 1]).Value2 is null, then it doesn't have a function ToString() is not a function, so trying to execute it doesn't work. Does it say what type of exception it is? because there might be a bigger problem...

将null值应用于字符串是可行的,但是如果((Excel.Range)workSheet.Cells [currentRow,1])。Value2为null,那么它没有函数ToString()不是函数,所以尝试执行它不起作用。它说它是什么类型的例外?因为可能存在更大的问题......

#2


1  

In regards to the above post (I don't have enough reputation to comment on it)

关于上述帖子(我没有足够的声誉对其发表评论)

For the sake of helping other lost souls, I upped this post and will just add:

为了帮助其他失去的灵魂,我增加了这篇文章并将添加:

if (((Excel.Range)workSheet.Cells[currentRow, 1]).Value2 != null) {...}.
– Sylvain Feb 16 '15 at 21:07

if(((Excel.Range)workSheet.Cells [currentRow,1])。Value2!= null){...}。 - Sylvain于2015年2月16日21:07

I had an issue and used this, but found that, in my case at least, I changed this to:

我有一个问题,并使用了这个,但发现,至少在我的情况下,我改为:

if (excelWorksheet.Cells[row, column].Value2 != null)
{
  return ((Excel.Range)excelWorksheet.Cells[row, column]).Value2.ToString();
}
return "No data";

which worked for my particular case, at least. Having the Excel.Range portion in the if created errors.

至少对我的特殊情况有效。在if创建的错误中包含Excel.Range部分。

#1


9  

Applying a null value to a string is doable, but if ((Excel.Range)workSheet.Cells[currentRow, 1]).Value2 is null, then it doesn't have a function ToString() is not a function, so trying to execute it doesn't work. Does it say what type of exception it is? because there might be a bigger problem...

将null值应用于字符串是可行的,但是如果((Excel.Range)workSheet.Cells [currentRow,1])。Value2为null,那么它没有函数ToString()不是函数,所以尝试执行它不起作用。它说它是什么类型的例外?因为可能存在更大的问题......

#2


1  

In regards to the above post (I don't have enough reputation to comment on it)

关于上述帖子(我没有足够的声誉对其发表评论)

For the sake of helping other lost souls, I upped this post and will just add:

为了帮助其他失去的灵魂,我增加了这篇文章并将添加:

if (((Excel.Range)workSheet.Cells[currentRow, 1]).Value2 != null) {...}.
– Sylvain Feb 16 '15 at 21:07

if(((Excel.Range)workSheet.Cells [currentRow,1])。Value2!= null){...}。 - Sylvain于2015年2月16日21:07

I had an issue and used this, but found that, in my case at least, I changed this to:

我有一个问题,并使用了这个,但发现,至少在我的情况下,我改为:

if (excelWorksheet.Cells[row, column].Value2 != null)
{
  return ((Excel.Range)excelWorksheet.Cells[row, column]).Value2.ToString();
}
return "No data";

which worked for my particular case, at least. Having the Excel.Range portion in the if created errors.

至少对我的特殊情况有效。在if创建的错误中包含Excel.Range部分。