Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8553661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:42:12+00:00 2026-06-11T14:42:12+00:00

This is the function: private List<string> getLinks(HtmlAgilityPack.HtmlDocument document) { List<string> mainLinks = new List<string>();

  • 0

This is the function:

private List<string> getLinks(HtmlAgilityPack.HtmlDocument document)
        {

                List<string> mainLinks = new List<string>();
                var linkNodes = document.DocumentNode.SelectNodes("//a[@href]");
                if (linkNodes != null)
                {
                    foreach (HtmlNode link in linkNodes)
                    {
                        var href = link.Attributes["href"].Value;
                        if (href.StartsWith("http://") == true || href.StartsWith("https://") == true || href.StartsWith("www") == true) // filter for http 
                        {
                            mainLinks.Add(href);
                        }
                    }
                }

                return mainLinks;

        }

Sometimes the variable document is nuul in case the site have timeout not responde or the link is not in the right format lets say for the example the link is: wdfsfdgfsdg

So in the function test im doing:

private List<string> test(string url, int levels,DoWorkEventArgs eve)
        {

            levels = levelsTo;
            HtmlWeb hw = new HtmlWeb();
            List<string> webSites;
            try
            {
                this.Invoke(new MethodInvoker(delegate { Texts(richTextBox1, "Loading The Url:   " + url + "..." , Color.Red); }));
                HtmlAgilityPack.HtmlDocument doc =  to.GetHtmlDoc(url, reqOptions, null);
                if (timeOut == true)
                {
                    this.Invoke(new MethodInvoker(delegate { Texts(richTextBox1, " There Was A TimeOut" + Environment.NewLine , Color.Red); }));
                    timeOut = false;

                }
                else
                {
                    this.Invoke(new MethodInvoker(delegate { Texts(richTextBox1, " Done " + Environment.NewLine, Color.Red); }));
                }
                webSites = getLinks(doc);

So lets say the url is wdfsfdgfsdg then webSites is calling/using getLinks but since the url is wrong the variable doc is null so either here in the test function or in the getLinks function i need to handle this case. What i want to do is that it will tell the user that there was a timeout but also to continue the process to next the url. In the test function im calling the test function again and again like crawling and each time the variable url contai na different url.

This is the line im doing the crawling:

csFiles.AddRange(test(t, levels - 1, eve));

csFiles is a local List

So each time url contain another link and then trying to get the links of this website.
But since doc is null and its going to the function getLinks so in getLinks on the line:

var linkNodes = document.DocumentNode.SelectNodes("//a[@href]");

Im getting null exception and the program stop. The null is since document is null.

So how can i handle this case and make the program to continue to the next link ? And not to stop since its null and there is an exception.

If its i will update the question and add the full test function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:42:14+00:00Added an answer on June 11, 2026 at 2:42 pm

    Well, it should be as simple as checking for null.

    var linkNodes;
    
    if(document != null)
    {
        linkNodes= document.DocumentNode.SelectNodes("//a[@href]");
        // other things if document is not null
    }
    else
    {
        // handle null case
    }
    

    If you’re dependent on information from document, then you can either try to get the information from somewhere else, or abort the operation. there’s not much more you can do

    You can also use try / catch

    try
    {
        //some code here
    }
    catch(Exception ex)
    {
        //log exception, display error to user, or handle exception some way
    }
    finally
    {
        optional block.  clean up resources
    }
    

    I would depending on try/catch if you can avoid having an exception altogether by simply checking for null.

    try/catch is better for unexpected exceptions that you have to handle, or for exceptions that you don’t have control over.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Check this function. private static IEnumerable<string> FindAccessibleDatabases() { var connectionStrings = new List<string>(); Parallel.For(0,
This is the function: private void ParseAndDisplayXml(string filename) { XDocument document = XDocument.Load(filename); var
So given this input string: =?ISO-8859-1?Q?TEST=2C_This_Is_A_Test_of_Some_Encoding=AE?= And this function: private string DecodeSubject(string input) {
I have a function like this in actionscript3 private function uploadFile(event:MouseEvent):void { var uploader:URLRequest
I have a ComboBox with a label function like this one: private function fieldLabelFunction(item:Object):String
I have an array of function callbacks, like this: class Blah { private var
private List<String> longStr = new java.util.ArrayList<String>(); private List aList = null; private String []
private List<string> retrieveImages(string address) { System.Net.WebClient wc = new System.Net.WebClient(); List<string> imgList = new
i have a function: private IEnumerable<Promotion> MatchesKeyword(IEnumerable<Promotion> list, String keyword) { ...snip... } which
I have a private function like this in my controller. private UserDetails GetUserDetails(int userid)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.