【问题标题】:Why is my code returning a Null Object Refrence error when using WatIn?为什么我的代码在使用 WatIn 时返回空对象引用错误?
【发布时间】:2011-11-19 14:57:35
【问题描述】:

我不断收到空对象引用错误,但不知道为什么。 我有一个包含 100 个网址的 CSV 文件。该文件被读入一个名为“lines”的数组中。

public partial class Form1 : System.Windows.Forms.Form
{
    string[] lines;
    public Form1() ...    

private void ReadLinksIntoMemory()
    {
        //this reads the chosen csv file into our "lines" array
        //and splits on comma's and new lines to create new objects within the array
        using (StreamReader sr = new StreamReader(@"C:\temp.csv"))
        {
            //reads everything in our csv into 1 long line
            string fileContents = sr.ReadToEnd();

            //splits the 1 long line read in into multiple objects of the lines array
           lines = fileContents.Split(new string[] { ",", Environment.NewLine },
                StringSplitOptions.RemoveEmptyEntries);

            sr.Dispose();
        }
    }

下一部分是我得到空对象错误的地方。当我尝试使用 WatIn 转到行数组中的第一项时,它说我正在引用一个空对象。

private void GoToEditLinks()
    {                        
        for (int i = 0; i < lines.Length; i++)
        {
            //go to each link sequentially
            myIE.GoTo(lines[i].ToString());

            //sleep so we can make sure the page loads
            System.Threading.Thread.Sleep(5000);                  
        }
    }

当我调试代码时,它说 GoTo 请求调用了为空的行。

似乎我需要声明数组,但我不需要告诉它一个确切的大小来做到这一点吗?示例:

lines = new string[10]

我以为我可以使用lines.Length 来告诉它制作数组有多大,但这不起作用。对我来说奇怪的是我可以毫无问题地使用以下代码:

//returns the accurate number of urls that were in the CSV we read in earlier
txtbx1.text = lines.Length;

//or
//this returns the last entry in the csv, as well as the last entry in the array
TextBox2.Text = lines[lines.Length - 1];

我很困惑为什么数组中明显有项目,可以调用它们来填充文本框,但是当我尝试在我的 for 循环中调用它们时,它说它是一个空引用?

更新: 通过将光标放在两个调用行上并按 f12,我发现它们都转到了同一个实例。接下来的想法是我没有及时调用 ReadLinksIntoMemory,下面是我的代码:

private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;

        ReadLinksIntoMemory();

        GoToEditLinks();

        button1.Enabled = true;

    }

除非我弄错了,否则代码说必须在调用 GoToEditLinks 之前完成 ReadLinksIntoMemory 方法?如果 ReadLinksIntoMemory 没有及时完成,我应该无法用行数组长度和/或最后一个条目填充我的文本框。

更新:进入方法 GoToEditLinks() 我看到在它调用之前行是空的:

myIE.GoTo(lines[i]);

但是当它点击 goto 命令时,值会从 null 变为它应该转到的 url,但同时它给了我 null 对象错误?

更新: 我添加了一个 IsNullOrEmpty 检查方法,并且行数组通过它没有任何问题。我开始认为这是 WatIn 和 myIE.GoTo 命令的问题。

我认为这是堆栈跟踪/调用堆栈?

Program.exe!Program.Form1.GoToEditLinks() Line 284  C#
Program.exe!Program.Form1.button1_Click(object sender, System.EventArgs e) Line 191 + 0x8 bytes C#
[External Code] 
Program.exe!Program.Program.Main() Line 18 + 0x1d bytes C#
[External Code] 

解决方案: 原来问题是WatIn。在正常情况下,我已经初始化了 myIE 对象,但在这种情况下,我正在对这两种方法进行速记测试,这意味着尚未创建 myIE 对象!我首先运行了初始化 myIE 对象的方法,它起作用了。谢谢大家帮助我解决这个问题。

【问题讨论】:

  • 不是一个答案,但你在使用时不需要调用 dispose,因为 using 调用 dispose 本身
  • 数组是空的还是只有一行?如果它只是你总是可以使用的行:string.IsNullOrEmpty 在使用之前对其进行测试......
  • 您能否发布确切的错误详细信息,最好使用堆栈转储?
  • 所有行都不应该为空,通过读取 1 个 csv 然后删除任何不包含“编辑”一词的行来创建 csv。 csv 中有 100 个条目,lines.length 返回为 100。
  • 发生错误导致程序崩溃时,stackdump是不是“details”中包含的信息?

标签: c# arrays for-loop watin nullreferenceexception


【解决方案1】:

如果 ReadLinksIntoMemory() 先运行,前 2 个示例中的代码看起来没问题。它创建lines 数组。

试试这个:

  1. 将光标放在 ReadLinksIntoMemory 内的 lines 上,按 F12。它应该转到类顶部的声明。

  2. 将光标放在 GoToEditLink 内的 lines 上。它应该带您到相同的声明。

您会发现lines 有 2 个声明,或者您没有按时调用 ReadLinksIntoMemory()。

你不应该需要 lines = new string[10]


第 2 步:在 ReadLinksIntoMemory 和 GoToEditLink 的开头放置断点。确保第一个先断。

然后在 GoToEditLink 中,仔细检查 lines、lines.Length 和(部分)它的内容。

这种 String.Split() 的变体不应产生 null 条目,但仍然:

for (int i = 0; i < lines.Length; i++)
{
  //go to each link sequentially
  string line = lines[i]; 
  myIE.GoTo(line);   // set a breakpoint here with the Condition `line == null`. 

  ...
}

请注意,.ToString() 没有理由在来自 string[] 的元素上

【讨论】:

    【解决方案2】:

    无法从您的代码中看出为什么 lines 或 lines[i] 会为空,但这就是它的意思。 测试 null / IsNullOrEmpty,无论如何这是一个好习惯,添加一个 else 并抛出一个 ArgumentNullException,在上面进行调试并点击 go。事情可能会变得更清楚。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-07
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 2015-05-01
      • 2016-02-16
      • 2020-09-21
      • 1970-01-01
      相关资源
      最近更新 更多