Asp.net NullReferenceException and how fix it

What is a “NullReferenceException” and how fix it in Asp.net:



The runtime throwing a “NullReferenceException” always means the same thing. You are trying to use a reference to an object which isn't initialized. It means your code used an object reference variable (actual object instance) that was set to null.

When “NullReferenceException” occurs:

When we declare a variable but not initialized the value to declared variable.
Int a; If this case some time it’s possible that here occurs NullReferenceException.

Example of NullReferenceException:

Int a;
If(check1.selected)
{
a=20;
}
Response.wright(a);   //Exception can occur here.

How to prevent by NullReferenceException:

This means the reference points to null, on which you cannot access members.

The simplest case:

String foo = null;
foo.ToUpper();

This will throw a NullReferenceException at the second line, because you can't call the instance method ToUpper() on a string reference pointing to null.

Other Asp.net Example:


Checkboxlist control related old post :

Comments