0

I am using HTML Agility pack in Xamarin and I keep getting the following error. System.NullReferenceException: Object reference not set to an instance of an object.

The error is coming from the foreach loop and I saw that the nodes variable was null, but I don't know why. It is probably because my node selection is not right, but I don't how to fix it as I already tried different selections.

string source = getSource(url);
if (source == "") return;

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(source);

HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@id='events']");

var nodeText = "";

foreach (HtmlNode node in nodes)
{
    nodeText += node.InnerText;
}
lbl_geheel.Text = nodeText;

So I guess ("//div[@id='events']") is not right.

This is the HTML code that I am scraping:

<div id="events" class="">

From the following page: https://www.espn.in/football/scoreboard

J.K. Harthoorn
  • 218
  • 1
  • 3
  • 19
  • Does it have a way to select a **single** node perhaps? https://stackoverflow.com/questions/37130244/htmlagilitypack-how-to-get-the-tag-by-id – mjwills Jul 18 '19 at 11:13
  • @mjwills like this? `string source = getSource(url); if (source == "") return; HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(source); HtmlNode node = doc.DocumentNode.SelectSingleNode("//div[@id='events']"); lbl_geheel.Text = node.InnerText;` Still getting the same error. – J.K. Harthoorn Jul 18 '19 at 14:05
  • `HtmlDocument docHtml = new HtmlWeb().Load("https://www.espn.in/football/scoreboard"); HtmlNode node = docHtml.GetElementbyId("events"); Console.WriteLine(node.InnerText); Console.ReadLine();` works fine. _Note it outputs blank, but that is to be expected._ – mjwills Jul 18 '19 at 14:12
  • @mjwills I don't understand why it's empty, but I will read the post. Why did my original code returned `System.NullReferenceException: Object reference not set to an instance of an object.` and your code didn't? – J.K. Harthoorn Jul 18 '19 at 14:17
  • Because there was an issue in your selector. I am not _exactly_ sure what the issue was - but I know that if you have an id then using `GetElementById` is much simpler. – mjwills Jul 18 '19 at 14:18
  • @mjwills Alright. However, this particular id is not actually something I really needed. I just used this as example. Sometimes I need something that has no id, but has a class for example or a data-type. Let's say I want the text from `Text` then I cannot use `GetElementById`. How would I approach this? – J.K. Harthoorn Jul 18 '19 at 14:32
  • You'll need to write a new question for that, alas. But https://stackoverflow.com/a/10096253/34092 is the likely duplicate for it. Found by Googling for `html agility pack search for span by class`. Either way, be sure to check for `null` since HTML Agility Pack returns `null` rather than empty collections (as you have discovered)! – mjwills Jul 18 '19 at 14:34
  • @mjwills HTML Agility pack is so confusing... I took this line from the source code, not the dev tool, `Maruti Suzuki Arena Presents ESPN.in Free Kick Episode 11`, used this code `var node = doc.DocumentNode.SelectSingleNode("//a[@name='&lpos=soccer-scoreboard-now:video:2']"); lbl_geheel.Text = node.InnerText;` and got the same error.. This was the answer from the link that you've provided, but at the comments I saw the person who asked the question got the same error from that error. – J.K. Harthoorn Jul 18 '19 at 15:02

0 Answers0