对象引用未设置为htmlcollection的实例[duplicate]

时间:2022-03-16 14:12:18

This question already has an answer here:

这个问题在这里已有答案:

I need kind favor to signoff the task. I want to count the tag using htmlaglitypack. I tried to count the tag by using htmlcollection node. But getting

签署任务需要我的帮助。我想用htmlaglitypack计算标签。我试图通过使用htmlcollection节点来计算标记。但是得到

"Object reference not set to an instance of an object"

“你调用的对象是空的”

In the line foreach condition. Can anyone of them rectify the issue why I'm getting like that?

在foreach条件行。他们中的任何人都可以纠正这个问题,为什么我会这样做?

My code is posted below:

我的代码发布在下面:

public void XmlPPC(string rights)
{
    int count = 0;
    try
    {
        MessageBox.Show(rights);
        using (FileStream fs = File.Open(rights,
                                         FileMode.Open,
                                         FileAccess.Read,
                                         FileShare.ReadWrite))
        using (BufferedStream bs = new BufferedStream(fs))
        using (StreamReader sr = new StreamReader(bs))
        {
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.Load(sr);

            HtmlNodeCollection right = doc.DocumentNode.SelectNodes("//copyrightLine");
            foreach (HtmlNode logan in right)
            {
                 count = count + 1;
                 MessageBox.Show("cnt" + count.ToString());
            }

            // snip...
        }
    }
    catch (Exception f)
    {
        log = log + "\r\n" + f.ToString();
    }
}

1 个解决方案

#1


0  

You're getting the error:

你收到错误:

Object reference not set to an instance of an object.

你调用的对象是空的。

because this line:

因为这一行:

HtmlNodeCollection right = doc.DocumentNode.SelectNodes("//copyrightLine");

is returning null. That can only happen because there is no element named copyrightLine. Please consider the following specification for the // operation:

返回null。这只会发生,因为没有名为copyrightLine的元素。请考虑以下//操作规范:

Selects nodes in the document from the current node that match the selection no matter where they are.

从当前节点中选择与选择匹配的文档中的节点,无论它们在何处。

Now, the fix is one of a few things:

现在,修复是以下几点之一:

  1. Get an element in there named copyrightLine.
  2. 获取名为copyrightLine的元素。

  3. Fix the misspelling as it could be misspelled.
  4. 修复拼写错误,因为拼写错误。

  5. Search for what you need in a different way if it doesn't fall into those two.
  6. 如果它不属于这两种方式,则以不同的方式搜索您需要的内容。

#1


0  

You're getting the error:

你收到错误:

Object reference not set to an instance of an object.

你调用的对象是空的。

because this line:

因为这一行:

HtmlNodeCollection right = doc.DocumentNode.SelectNodes("//copyrightLine");

is returning null. That can only happen because there is no element named copyrightLine. Please consider the following specification for the // operation:

返回null。这只会发生,因为没有名为copyrightLine的元素。请考虑以下//操作规范:

Selects nodes in the document from the current node that match the selection no matter where they are.

从当前节点中选择与选择匹配的文档中的节点,无论它们在何处。

Now, the fix is one of a few things:

现在,修复是以下几点之一:

  1. Get an element in there named copyrightLine.
  2. 获取名为copyrightLine的元素。

  3. Fix the misspelling as it could be misspelled.
  4. 修复拼写错误,因为拼写错误。

  5. Search for what you need in a different way if it doesn't fall into those two.
  6. 如果它不属于这两种方式,则以不同的方式搜索您需要的内容。