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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:01:35+00:00 2026-05-26T22:01:35+00:00

I keep getting this error when I run the program. Object reference not set

  • 0

I keep getting this error when I run the program.

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line with error:

Line 156:        if (strSearch == "" || strSearch.Trim().Length == 0)

What is the correct way it should be written?

  • 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-05-26T22:01:37+00:00Added an answer on May 26, 2026 at 10:01 pm

    The correct way in .NET 4.0 is:

    if (String.IsNullOrWhiteSpace(strSearch))
    

    The String.IsNullOrWhiteSpace method used above is equivalent to:

    if (strSearch == null || strSearch == String.Empty || strSearch.Trim().Length == 0) 
    // String.Empty is the same as ""
    

    Reference for IsNullOrWhiteSpace method

    http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx

    Indicates whether a specified string is Nothing, empty, or consists
    only of white-space characters.

    In earlier versions, you could do something like this:

    if (String.IsNullOrEmpty(strSearch) || strSearch.Trim().Length == 0)
    

    The String.IsNullOrEmpty method used above is equivalent to:

    if (strSearch == null || strSearch == String.Empty)
    

    Which means you still need to check for your “IsWhiteSpace” case with the .Trim().Length == 0 as per the example.

    Reference for IsNullOrEmpty method

    http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx

    Indicates whether the specified string is Nothing or an Empty string.

    Explanation:

    You need to ensure strSearch (or any variable for that matter) is not null before you dereference it using the dot character (.) – i.e. before you do strSearch.SomeMethod() or strSearch.SomeProperty you need to check that strSearch != null.

    In your example you want to make sure your string has a value, which means you want to ensure the string:

    • Is not null
    • Is not the empty string (String.Empty / "")
    • Is not just whitespace

    In the cases above, you must put the “Is it null?” case first, so it doesn’t go on to check the other cases (and error) when the string is null.

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

Sidebar

Related Questions

I keep getting this error when I call 'tomcat:run'. The plugin 'org.apache.maven.plugins:maven-tomcat-plugin' does not
I am trying to run this program but i keep getting the error esent.h
I keep getting this error when I run my web app (asp.net mvc) in
I keep getting this error System.Web.HttpException was unhandled by user code Message=Validation of viewstate
I'm trying to run a very simple integration test and keep getting this error:
I keep getting this error: Object doesn't support this property or method whenever I
I keep getting this error whenever I run unit tests in IntelliJ IDEA: /home/egervari/IdeaProjects/jobprep-stable/src/main/resources/dict/noun.txt
I keep getting this error when I try and run this script. It has
Hi i'm trying to run Nant but keep getting this error when i tried
When I try to run privoxy, I keep getting this error can't check configuration

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.