having an error

Topics: User Forum
Feb 16, 2012 at 8:26 PM

I am trying to test this pack but at this line

   foreach (HtmlNode link in doc.DocumentNode.SelectNodes(@"//a[@href]"))

I receive this error "Object reference not set to an instance of an object."

Here is the code how I try to call it from my form.

  private void button1_Click(object sender, EventArgs e)
        {
           
        
           Uri someSite = new Uri("http://www.nationalgeographic.com/");
          // MessageBox.Show("Am deschis pagina");
            IEnumerable<WebPage> theSitePages = WebPage.GetAllPagesUnder(someSite);
           // MessageBox.Show("Acuma afisez rezultatele");
            listBox1.Items.AddRange(theSitePages.ToArray());

Feb 23, 2012 at 9:12 AM

It seems like you might not checking null before executing the above line

if (doc.DocumentNode.SelectNodes(@"//a[@href]" != null){

    foreach (HtmlNode link in doc.DocumentNode.SelectNodes(@"//a[@href]")){

   //your code goes here
}

}

please share some more code too if above does not work.

Please let me know in case of any concerns.

Feb 23, 2012 at 11:42 AM

I tried to use it like this

  if (doc.DocumentNode.SelectNodes(@"//a[@href]" != null)) //I've put the code you've written here
                    {
                    // And and now queue up all the links on this page
                    foreach (HtmlNode link in doc.DocumentNode.SelectNodes(@"//a[@href]"))
                    {
                        HtmlAttribute att = link.Attributes["href"];
                        if (att == null) continue;
                        string href = att.Value;
                        if (href.StartsWith("javascript", StringComparison.InvariantCultureIgnoreCase)) continue;      // ignore javascript on buttons using a tags

                        Uri urlNext = new Uri(href, UriKind.RelativeOrAbsolute);

                        // Make it absolute if it's relative
                        if (!urlNext.IsAbsoluteUri)
                        {
                            urlNext = new Uri(urlRoot, urlNext);
                        }

                        if (!allSiteUrls.Contains(urlNext))
                        {
                            allSiteUrls.Add(urlNext);               // keep track of every page we've handed off

                            if (urlRoot.IsBaseOf(urlNext))
                            {
                                queue.Enqueue(urlNext);
                            }
                            else
                            {
                                yield return new WebPage.External() { Url = urlNext };
                            }

but is not working

I have these errors

Error 1 The best overloaded method match for 'HtmlAgilityPack.HtmlNode.SelectNodes(string)' has some invalid arguments D:\situri\baze de date\htmlAgility\htmlAgility\test.cs 66 25 htmlAgility

Error 2 Argument 1: cannot convert from 'bool' to 'string' D:\situri\baze de date\htmlAgility\htmlAgility\test.cs 66 54 htmlAgility