Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object reference not set to an instance of an object. [duplicate]

Tags:

c#

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?

like image 248
Mike Avatar asked Nov 21 '11 03:11

Mike


People also ask

How do I fix object reference is not set to an instance of an object?

To fix "Object reference not set to an instance of an object," you should try running Microsoft Visual Studio as an administrator. You can also try resetting the user data associated with your account or updating Microsoft Visual Studio to the latest version.

How do you resolve an object reference not set to an instance of an object in VB net?

"instance of object" Means that the object is blank (or in VB speak, "Nothing"). When you are dealing with object variables, you have to create an instance of that object before referencing it. "not set to an " means that you tried to access an object, but there was nothing inside of it for the computer to access.

What is the meaning of object reference not set to an instance of an object in C#?

So, this error description says that an object that is being called to get or set its value has no reference. This means that you are trying to access an object that was not instantiated.


2 Answers

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.

like image 67
Matt Mitchell Avatar answered Sep 21 '22 03:09

Matt Mitchell


All versions of .Net:

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

.Net 4.0 or later:

if (String.IsNullOrWhitespace(strSearch)) 
like image 32
Ritch Melton Avatar answered Sep 22 '22 03:09

Ritch Melton