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 8178629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:48:47+00:00 2026-06-06T23:48:47+00:00

How do I catch the NullReferenceException Error in the foreach loop below if ‘SelectNodes’

  • 0

How do I catch the NullReferenceException Error in the foreach loop below if ‘SelectNodes’ returns NULL?

I searched on stackoverflow and found mention of the null-coalescing condition (?? condition) that can be used to catch this error, however, I have no idea on what the syntax would be for HTMLNode, or if that’s even possible.

foreach (HtmlNode link in imagegallery.DocumentNode.SelectNodes("//a[@href]") )
            {
                //Do Something
            }

How would you cath the NULL EXCEPTION for this loop, or is there a better way of doing this?

Here is the full code throwing the exception –

    private void TEST_button1_Click(object sender, EventArgs e)
    {
        //Declarations           
        HtmlWeb htmlWeb = new HtmlWeb();
        HtmlAgilityPack.HtmlDocument imagegallery;

            imagegallery = htmlWeb.Load(@"http://adamscreation.blogspot.com/search?updated-max=2007-06-27T10:03:00-07:00&max-results=20&start=18&by-date=false");

            foreach (HtmlNode link in imagegallery.DocumentNode.SelectNodes("//a[@imageanchor=1 or contains(@href,'1600')]/@href"))
            {
               //do something
            }
    }       
  • 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-06T23:48:48+00:00Added an answer on June 6, 2026 at 11:48 pm
    if(imagegallery != null && imagegallery.DocumentNode != null){
      foreach (HtmlNode link in 
        imagegallery.DocumentNode.SelectNodes("//a[@href]") 
          ?? Enumerable.Empty<HtmlNode>()) 
      {
        //do something
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code: catch (IOException e) { LOGGER.error(IOException exception happened); //now need throw again the same
I want to catch a fancybox error (The requested content cannot be loaded. Please
Why would my try-catch block still be throwing an error when it's handled? Exception
I keep getting a null object reference error when im running one of my
I'm trying to catch a JSON object with a mouse click event. I use
I'd like to catch exception which occurs when somebody gives not a number value
I need to catch or filter a number from string example: var Mystring =
How to catch return hit (enter) on TextBox? The following does not work. I
i want to catch the data from server as an answer and alert it.
I need to catch two exceptions because they require the same handling logic. I

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.