C#/ Html Agility pack error "Value cannot be null. Parameter name: Source."

c#-4.0 html-agility-pack

Question

I've used html agility pack before, and have had good results with a little bit of trial and error. I'm currently trying to use it to return a node set with an xpath I get by right- clicking "Copy XPath" in Firefox. I've done some searching, and I see that the browser will often add "tbody" for table tags. I tried it with removing this with no luck. Here is the xpath given to me by Firefox:

/html/body/p[3]/table/tbody/tr/td/table/tbody/tr[3]

Using it as- is throws the error: "Value cannot be null. Parameter name: source."

This occurs on line:

nodeList = htmlDoc.DocumentNode.SelectNodes("/html/body/p[3]/table/tbody/tr/td/table/tbody/tr[3]").ToList();

I'll continue to read, in the meantime if this is an easy fix to anyone, I'd appreciate a tip.

Update: This is the actual code:

protected override List<IDataPoint> ReturnDataPointsFromIndividualAddressString(string AddressString)
{
       List<IDataPoint> earningsAnnouncements = new List<IDataPoint>(); //Not used, yet..

       HtmlWeb hwObject = new HtmlWeb();
       HtmlDocument htmlDoc = hwObject.Load(AddressString);

       if (htmlDoc.DocumentNode != null)
       {
               List<HtmlNode> nodeList = new List<HtmlNode>();
               nodeList = htmlDoc.DocumentNode.SelectNodes("/html/body/p[3]/table/tbody/tr/td/table/tbody/tr[3]").ToList();
       }
}

Popular Answer

It seems this error occurs on this line:

nodeList = htmlDoc.DocumentNode.SelectNodes("/html/body/p[3]/table/tbody/tr/td/table/tbody/tr[3]").ToList();

The thing is, if SelectNodes method doesn't find nodes by xpath expression passed it returns null. You could find more information in this answer to a similar question HTML Agility Pack Null Reference. And then you call a ToList() method on a null object which is actually causes an NullReferenceException.

To avoid this check the this variable against null like this:

var nodes = htmlDoc.DocumentNode.SelectNodes(...);
if (nodes != null)
{
     nodeList = nodes.ToList();
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why