Avoiding a NullReferenceException
        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:21 UTC
        
        
        Read the original article
        Hit count: 368
        
ASP.NET
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