0

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;
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • 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 May 11 '14 at 15:06
  • I used chrome browser and pressed F12 to look up the xpath id so I think the xpath id is valid. – user1637158 May 11 '14 at 15:12
  • Test the result of SelectNodes before you access it. – Steve Wellens May 11 '14 at 15:13
  • @user1637158 This is presumebly because [Chrome works against the DOM not the basic HTML source code](http://stackoverflow.com/questions/18241029/why-does-my-xpath-query-scraping-html-tables-only-work-in-firebug-but-not-the). There may be discrepancies. – User 12345678 May 11 '14 at 15:13

1 Answers1

-1

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.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Tomasz Kowalczyk May 11 '14 at 17:40
  • Thanks for sharing your opinion. Note I pointed out the most likely problem, gave advice on asking better questions and suggested a better programming style to improve ease of debugging. If he can't solve the problem with that information, I'm afraid spoon feeding him the answer will not really be helpful to anyone in the long term. – Steve Wellens May 11 '14 at 22:32
  • Thanks for the thoughtful reply, I voted for this answer in the review process (didn't downvote) and it seems that question should be closed as "insufficient information". – Tomasz Kowalczyk May 12 '14 at 09:27