Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi on this code i've tried to get images,it give me some image but after that show me this :Object reference not set to an instance of an object.
C#
HtmlAgilityPack.HtmlDocument html_doc = new HtmlAgilityPack.HtmlDocument();
foreach (var i in rssFeed)
{
    //---------------------links----------------------//
    html_doc.LoadHtml(i.Encoded.ToString());
    var link_html = html_doc.DocumentNode.SelectNodes("//a");
    foreach (var link in link_html)
    {
            if (link.Attributes["href"].Value.IndexOf(".jpg") > -1)
            {
                image_links.Add(link.Attributes["href"].Value);
            }
    }
Posted

1 solution

Looks like some of the anchor-nodes in link_html do not have "href"-Tags (link.Attributes["href"]). This might generate the NullReferenceException you are getting during the foreach.

Check if Value is set before using it.
C#
String.IsNullOrEmpty(link.Attributes["href"])
for example
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900