0
votes

I am trying to get a company's sector on yahoo finance using HTML Agility Pack but I keep getting object reference not set to instance of an object exception. Why does my code throw this exception? I already double checked the xpath Id numerous times.

string Url = "http://www.finance.yahoo.com/q/pr?s=MSFT+Profile";
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(Url);
string xpathid = "//*[@id=\"yfncsumtab\"]/tbody/tr[2]/td[1]/table[2]/tbody/tr/td/table/tbody/tr[2]/td[2]/a";
string sector = doc.DocumentNode.SelectNodes(xpathid)[0].InnerText;
Console.WriteLine(sector);

this is the line that is throwing the exception:

string sector = doc.DocumentNode.SelectNodes(xpathid)[0].InnerText;
1
Basically, your XPath is not matching any elements and therefore, when you attempt to access the InnerText of a non-existing (a null) element - a NullReferenceException is thrown. - User 12345678
I used chrome browser and pressed F12 to look up the xpath id so I think the xpath id is valid. - user1637158
Test the result of SelectNodes before you access it. - Steve Wellens
@user1637158 This is presumebly because Chrome works against the DOM not the basic HTML source code. There may be discrepancies. - User 12345678

1 Answers

-1
votes

Probably because SelectNodes is returning null...but you are trying to access it anyways.

You need to state which line is throwing the exception.

Jamming several operations into one line of code makes debugging more difficult than it needs to be.

[edit] Your updated post confirms what I suggested.