0

I found a question posted here that had something interesting i wanted to implement in my application.

Previous question(this is using HtmlAgilityPack): Pulling data from a webpage, parsing it for specific pieces, and displaying it

I've done pretty much what they are doing their and i'm getting an error stating object reference not set to an instance of an object. i'm fairly new to programming in general and while I've seen this error message hundreds of times i never seem to understand the cause or how to fix it. Can someone assist with this?

Code:

public void Page_Load()
        {
            string Url = "myPrivateURL";
            HtmlWeb web = new HtmlWeb();
            HtmlDocument doc = web.Load(Url);
//Both below strings are getting a null reference exception.
            string DeskOncallPhone = doc.DocumentNode.SelectNodes("//*[@id=\"content\"]/div/section/section/div[4]/div[1]/div[1]/div[2]/div/div/div[1]/span")[0].InnerText;
            string DeskOncallName = doc.DocumentNode.SelectNodes("//*[@id=\"content\"]/div/section/section/div[4]/div[1]/div[1]/div[2]/div/a[1]")[0].InnerText; 
        }

Error: enter image description here

Community
  • 1
  • 1
  • [Edit](http://stackoverflow.com/posts/43529796/edit) your question to include the error message. Then show us where in your code you get the error. If the error is NullReferenceException, [read this](http://stackoverflow.com/questions/4660142/). – Dour High Arch Apr 20 '17 at 21:03
  • so i added a comment in the code to more clearly specify where the error is occurring. To be clear the variable of DeskOncallPhone and DeskOncallName are not used anywhere else in the code and in the code i provided in the question they are not set to Null. i've read over the article you supplied twice from what i understand it only happens if something is set to null. my string url is set to my URL which i choose to leave out but it is correctly set in code. doc = the URL loaded which is correct and both variables in the code = the selected node in doc. nothing is null. – firehotguitar88 Apr 21 '17 at 01:26
  • It states that HelpLink is null and innerException is also null. what and where is helplink and innerexception? – firehotguitar88 Apr 21 '17 at 01:29
  • The problem is shown in `StackTrace`; you should examine that to see what is wrong. My guess is the data from `myPrivateUrl` doesn't contain a node matching `//*[@id=\"content\"]/div/section/section/div[4]/div[1]/div[1]/div[2]/div/div/div[1]/span` and not because “something is set to null”. If you think that's what [What is a NullReferenceException](http://stackoverflow.com/questions/4660142/) means, you didn't understand it. Start by breaking that huge XQuery expression into segments and test each segment in a debugger. – Dour High Arch Apr 21 '17 at 18:01

0 Answers0