나는 이런 것을 가지고있다 :
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;
}
}
}
이제 프로그램을 시작할 때 Object reference not set to an instance of an object.
가 Object reference not set to an instance of an object.
않았다는 오류가 발생하면 if
sttement에 오류가 발생 Object reference not set to an instance of an object.
. 내 페이지가로드되지 않는 이유는 무엇입니까? 여기서 내가 뭘 잘못하고 있니?
.Load()
찾고 있습니다.
.LoadHtml()
에는 실제 HTML이 있어야합니다. 당신은 웹 사이트를 제공하고 있습니다 :
HtmlWeb website = new HtmlWeb();
HtmlDocument rootDocument = website.Load("http://www.example.com");
Arran의 대답 외에도
.SelectNodes("//span[@class='some-class']")
가 노드를 반환하지 않고 null
경우 Count
를 수행하면이 예외가 발생합니다.
시험
if (task.Document.DocumentNode.SelectNodes("//span[@class='some-class']") != null &&
task.Document.DocumentNode.SelectNodes("//span[@class='some-class']").Count == 0)
{
task.IsBusy = false;
return;
}