Rectangle 27 92

The correct way in .NET 4.0 is:

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

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

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.

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

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 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.

You are missing the closing ) of the if

It's nice to have a class Extension method to do this check: public static IsEmptyOrWhiteSpace(this string value) { return String.IsEmptyOrWhiteSpace(value);

Would you be able to help me with an issue that I am experiencing as well?

c# - Object reference not set to an instance of an object. - Stack Ove...

c#
Rectangle 27 92

The correct way in .NET 4.0 is:

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

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

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.

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

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 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.

You are missing the closing ) of the if

It's nice to have a class Extension method to do this check: public static IsEmptyOrWhiteSpace(this string value) { return String.IsEmptyOrWhiteSpace(value);

Would you be able to help me with an issue that I am experiencing as well?

c# - Object reference not set to an instance of an object. - Stack Ove...

c#
Rectangle 27 109

Variables in .NET are either reference types or value types. Value types are primitives such as integers and booleans or structures (and can be identified because they inherit from System.ValueType). Boolean variables, when declared, have a default value:

bool mybool;
//mybool == false

Reference types, when declared, do not have a default value:

class ExampleClass
{
}

ExampleClass exampleClass; //== null

The following code is a simple way of reproducing this:

This is a very common error and can occur because of all kinds of reasons. The root cause really depends on the specific scenario that you've encountered.

If you are using an API or invoking methods that may return null then it's important to handle this gracefully. The main method above can be modified in such a way that the NullReferenceException should never be seen by a user:

static void Main(string[] args)
{
    var exampleClass = new ExampleClass();
    var returnedClass = exampleClass.ExampleMethod();

    if (returnedClass == null)
    {
        //throw a meaningful exception or give some useful feedback to the user!
        return;
    }

    returnedClass.AnotherExampleMethod();
}

All of the above really just hints of .NET Type Fundamentals, for further information I'd recommend either picking up CLR via C# or reading this MSDN article by the same author - Jeffrey Richter. Also check out, much more complex, example of when you can encounter a NullReferenceException.

Some teams using Resharper make use of JetBrains attributes to annotate code to highlight where nulls are (not) expected.

Run-time exception (line 9): Object reference not set to an instance of an object.

Since the tag is .net but the language is not specified, in VB.Net the common error is to simply say Dim exampleClass As exampleClass rather than Dim exampleClass As New exampleClass.

.net - What does "Object reference not set to an instance of an object...

.net nullreferenceexception
Rectangle 27 21

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

c# - Object reference not set to an instance of an object. - Stack Ove...

c#
Rectangle 27 21

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

c# - Object reference not set to an instance of an object. - Stack Ove...

c#
Rectangle 27 145

devenv /resetuserdata

Expect, that all of your user settings such as Visual Studio layout, linked Microsoft account or start page dissapear.

Worked!! However, I just renamed the Visualstudio & VSCommon dirs to VisualStudio_old & VSCommon_old. No need to run "devenv /resetuserdata"

@DamianKobak - yes, it often helps, but only for a specific solution. When things go wrong on every project steps in answer suit better.

Actually it is sufficient do delete folder ComponentModelCache on path C:\Users\{user}\AppData\Local\Microsoft\VisualStudio\{version - 14 or 12}. You can keep the rest and no settings get lost...

I can +1 to @Tom Burgers comment above. And if you don't want to remember the path and file name for next time you can use Mads Kristensens excellent extension Clear Component Cache (github.com/madskristensen/ClearComponentCache).

c# - Visual Studio 2015 error "Object reference not set to an instance...

c# asp.net asp.net-mvc-4 razor visual-studio-2015
Rectangle 27 15

I would guess that Session is the culprit here; with reference here, you might want to try adding : IRequiresSessionState to your handler (the code-behind for the ashx). So you should have something like:

public class Handler1 : IHttpHandler, System.Web.SessionState.IRequiresSessionState 
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
        context.Session["loggedIn"] = true;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Note also that it is easier to talk to the context passed in, but HttpContext.Current should work too.

asp.net - c# sessions "Object reference not set to an instance of an o...

c# asp.net session
Rectangle 27 13

There is a difference between

List<string> list;

and

List<string> list = new List<string>();

When you didn't use new keyword in this case, your list didn't initialized. And when you try to add it hai, obviously you get an error.

c# - How to solve Object reference not set to an instance of an object...

c# asp.net nullreferenceexception
Rectangle 27 12

Person myPet = GetPersonFromDatabase();
 // check for myPet == null... AND for myPet.PetType == null
 if ( myPet.PetType == "cat" ) <--- fall down go boom!

Jay's answer demonstrates another example that isn't covered by any of the other answers here.

.net - What does "Object reference not set to an instance of an object...

.net nullreferenceexception
Rectangle 27 10

Not to be blunt but it means exactly what it says. One of your object references is NULL. You'll see this when you try and access the property or method of a NULL'd object.

He can be doing throw new NullReferenceException(); lol...

It means exactly what it says IF you know what an object is, a reference and instance... all very loaded words when it comes to programming.

.net - What does "Object reference not set to an instance of an object...

.net nullreferenceexception
Rectangle 27 9

Thanks .... Nu-get Facebook package upgrade solved the issue

c# - Facebook Login Asp.net Mvc Object reference not set to an instanc...

c# asp.net-mvc oauth facebook-oauth
Rectangle 27 4

The error line "Object reference not set to an instance of an object. " states that you have not assigned instance object to a object reference and still you are accessing properies/methods of that object.

for example: let say you have a class called myClass and it contains one property prop1.

public Class myClass
{
   public int prop1 {get;set;}
}
public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref.prop1 = 1;  //This line throws error
     }
}

above line throws error because reference of class myClass is declared but not instantiated or an instance of object is not assigned to referecne of that class.

To fix this you have to instantiate (assign object to reference of that class).

public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref = new myClass();
        ref.prop1 = 1;  
     }
}

c# - What is a NullReferenceException, and how do I fix it? - Stack Ov...

c# .net vb.net null nullreferenceexception
Rectangle 27 4

The error line "Object reference not set to an instance of an object. " states that you have not assigned instance object to a object reference and still you are accessing properies/methods of that object.

for example: let say you have a class called myClass and it contains one property prop1.

public Class myClass
{
   public int prop1 {get;set;}
}
public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref.prop1 = 1;  //This line throws error
     }
}

above line throws error because reference of class myClass is declared but not instantiated or an instance of object is not assigned to referecne of that class.

To fix this you have to instantiate (assign object to reference of that class).

public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref = new myClass();
        ref.prop1 = 1;  
     }
}

c# - What is a NullReferenceException, and how do I fix it? - Stack Ov...

c# .net vb.net null nullreferenceexception
Rectangle 27 4

The error line "Object reference not set to an instance of an object. " states that you have not assigned instance object to a object reference and still you are accessing properies/methods of that object.

for example: let say you have a class called myClass and it contains one property prop1.

public Class myClass
{
   public int prop1 {get;set;}
}
public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref.prop1 = 1;  //This line throws error
     }
}

above line throws error because reference of class myClass is declared but not instantiated or an instance of object is not assigned to referecne of that class.

To fix this you have to instantiate (assign object to reference of that class).

public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref = new myClass();
        ref.prop1 = 1;  
     }
}

c# - What is a NullReferenceException, and how do I fix it? - Stack Ov...

c# .net vb.net null nullreferenceexception
Rectangle 27 4

The error line "Object reference not set to an instance of an object. " states that you have not assigned instance object to a object reference and still you are accessing properies/methods of that object.

for example: let say you have a class called myClass and it contains one property prop1.

public Class myClass
{
   public int prop1 {get;set;}
}
public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref.prop1 = 1;  //This line throws error
     }
}

above line throws error because reference of class myClass is declared but not instantiated or an instance of object is not assigned to referecne of that class.

To fix this you have to instantiate (assign object to reference of that class).

public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref = new myClass();
        ref.prop1 = 1;  
     }
}

c# - What is a NullReferenceException, and how do I fix it? - Stack Ov...

c# .net vb.net null nullreferenceexception
Rectangle 27 4

The error line "Object reference not set to an instance of an object. " states that you have not assigned instance object to a object reference and still you are accessing properies/methods of that object.

for example: let say you have a class called myClass and it contains one property prop1.

public Class myClass
{
   public int prop1 {get;set;}
}
public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref.prop1 = 1;  //This line throws error
     }
}

above line throws error because reference of class myClass is declared but not instantiated or an instance of object is not assigned to referecne of that class.

To fix this you have to instantiate (assign object to reference of that class).

public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref = new myClass();
        ref.prop1 = 1;  
     }
}

c# - What is a NullReferenceException, and how do I fix it? - Stack Ov...

c# .net vb.net null nullreferenceexception
Rectangle 27 4

The error line "Object reference not set to an instance of an object. " states that you have not assigned instance object to a object reference and still you are accessing properies/methods of that object.

for example: let say you have a class called myClass and it contains one property prop1.

public Class myClass
{
   public int prop1 {get;set;}
}
public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref.prop1 = 1;  //This line throws error
     }
}

above line throws error because reference of class myClass is declared but not instantiated or an instance of object is not assigned to referecne of that class.

To fix this you have to instantiate (assign object to reference of that class).

public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref = new myClass();
        ref.prop1 = 1;  
     }
}

c# - What is a NullReferenceException, and how do I fix it? - Stack Ov...

c# .net vb.net null nullreferenceexception
Rectangle 27 4

The error line "Object reference not set to an instance of an object. " states that you have not assigned instance object to a object reference and still you are accessing properies/methods of that object.

for example: let say you have a class called myClass and it contains one property prop1.

public Class myClass
{
   public int prop1 {get;set;}
}
public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref.prop1 = 1;  //This line throws error
     }
}

above line throws error because reference of class myClass is declared but not instantiated or an instance of object is not assigned to referecne of that class.

To fix this you have to instantiate (assign object to reference of that class).

public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref = new myClass();
        ref.prop1 = 1;  
     }
}

c# - What is a NullReferenceException, and how do I fix it? - Stack Ov...

c# .net vb.net null nullreferenceexception
Rectangle 27 1

object reference not set to an instance of an object has always been an un initialized list for me. try initializing recommendedevents before setting it. something like

List<Events> recommendedevents = new List<Events>();

replacing Events with whatever the type is.

the first parameter in Html.Partial is the partial name not the method call. you need to either pass the model to your view thought a view model and pass it to the partial

@Html.Partial("_RecommendedEvents", Model.Events)

c# - MVC4 Partial View Error "Object reference not set to an instance ...

c# asp.net-mvc asp.net-mvc-4 razor