Removing exception

Posted by Nikhil K on Stack Overflow See other posts from Stack Overflow or by Nikhil K
Published on 2010-03-20T16:06:07Z Indexed on 2010/03/20 16:11 UTC
Read the original article Hit count: 152

Filed under:

I have used this code for extracting urls from web page.But in the line of 'foreach' 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);
}

© Stack Overflow or respective owner

Related posts about ASP.NET