0

I'm new in C#. I need to pars link from category and subcategory.

I have a category, but when i try to take subcategory i have error:

"Object reference not set to an instance of an object".

My code:

foreach (var link in xpathLinks)
        {

                okno.Text += link.Attributes["href"].Value + "\n";
                string doc2 = HtmlPage.DownloadString("htmlPage" + link.
                    Attributes["href"].Value);
                dokument2.LoadHtml(doc2);
                HtmlNodeCollection xpathLinks2 = dokument2.DocumentNode.
                    SelectNodes("//ul[@class='category-nav']//li//a");
                foreach (var Link in xpathLinks2)
                {
                    okno.Text += Link.Attributes["href"].Value + "\n";
                }

            }
        }

I'm usin HtmlAgilityPack. The error is in xpathLinks2.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • [What is a `NullReferenceException` and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül Apr 29 '15 at 14:47
  • `NullReferenceException` is a common situation for beginner programmers. The link provided should help you understand the problem. Then use the debugger to find what/where/when you have a variable that is `null`. – Soner Gönül Apr 29 '15 at 14:47
  • Yes, but xpathLinks2 is taking value from website, and i can't give there any value. – Rafał Drozd Apr 29 '15 at 14:55
  • Question is not yet good fit for SO - please consider fixing following issues with the post: missing data necessary to demonstrate the problem, missing steps already taken to investigate NRE. Also make sure to read some basic XPath manual - i.e. some can be found by https://www.bing.com/search?q=msdn+xpath search. – Alexei Levenkov Apr 29 '15 at 14:55
  • OK, I finally find the problem. One of category has another class, and there was a problem. Thanks for help. :D – Rafał Drozd Apr 30 '15 at 07:38

0 Answers0