Avoiding a NullReferenceException
<p>I have used this code for extracting urls from web page.But in the line of 'foreach' it is showing </p>

<blockquote>
  <p>Object reference not set to an instance of an object</p>
</blockquote>

<p>exception.  What is the problem? how can i correct that?</p>

<pre><code>WebClient client = new WebClient();
string url = "http://www.google.co.in/search?hl=en&amp;q=java&amp;start=10&amp;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);
}
</code></pre>
