1

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

"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?

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();
    }
}
Nuffin
  • 3,882
  • 18
  • 34
Gan
  • 93
  • 1
  • 3
  • 11

1 Answers1

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:

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. Fix the misspelling as it could be misspelled.
  3. Search for what you need in a different way if it doesn't fall into those two.
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232