I have something like this:
class MyTask
{
public MyTask(int id)
{
Id = id;
IsBusy = false;
Document = new HtmlDocument();
}
public HtmlDocument Document { get; set; }
public int Id { get; set; }
public bool IsBusy { get; set; }
}
class Program
{
public static void Main()
{
var task = new MyTask(1);
task.Document.LoadHtml("http://urltomysite");
if (task.Document.DocumentNode.SelectNodes("//span[@class='some-class']").Count == 0)
{
task.IsBusy = false;
return;
}
}
}
Now when I start my program, it throws an error on the if sttement, saying that Object reference not set to an instance of an object.. Why isn't it loading my page? What am I doing wrong here?
You are looking for .Load().
.LoadHtml() expects to be given physical HTML. You are giving a website to go to:
HtmlWeb website = new HtmlWeb();
HtmlDocument rootDocument = website.Load("http://www.example.com");
In addition to Arran's answer
If .SelectNodes("//span[@class='some-class']") doesn't return any nodes and is null then doing a Count on it will give this exception.
Try
if (task.Document.DocumentNode.SelectNodes("//span[@class='some-class']") != null &&
task.Document.DocumentNode.SelectNodes("//span[@class='some-class']").Count == 0)
{
task.IsBusy = false;
return;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With