开发者

Avoiding a NullReferenceException

开发者 https://www.devze.com 2022-12-23 19:43 出处:网络
I have used this code for extracting urls from web page.But in the line of \'fore开发者_如何学Goach\' it is showing

I have used this code for extracting urls from web page.But in the line of 'fore开发者_如何学Goach' it is showing

Object reference not set to an instance of an object

exception. What is the problem? how can i correct that?

WebClient client = new WebClient();
string url = "http://www.google.co.in/search?hl=en&q=java&start=10&sa=N";
string source = client.DownloadString(url);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(source);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href and @rel='nofollow']"))
{
    Console.WriteLine(link.Attributes["href"].Value);
}


First, you should look up the NullReferenceException in the documentation. It says

The exception that is thrown when there is an attempt to dereference a null object reference.

This means you did the equivalent of

SomeClass reference = null;
reference.Method(); // or reference.Property;

Next, look at the line of code that has the error and figure out what you are derefencing:

foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href and @rel='nofollow']"))
  • doc.DocumentNode
  • doc.DocumentNode.SelectNodes

So either doc is null, or doc.DocumentNodes is null. Since you just assigned a new instance of HtmlDocument to doc, doc can't be the problem. That implies that you loaded an empty document, such that there is no doc.DocumentNode.

Check before the loop to see if doc.DocumentNode is null.


Which line is the exception being thrown from; the actual foreach line, or the contents of the loop?

Probably the easiest method to deal with this one is to use the debugger - pop a breakpoint in front of the foreach, and when the runtime pauses, inspect the contents of the various variables, such as link, doc.DocumentNode, etc. If link is non-null, then check whether link.Attributes["href"] is, and so on.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号