In. NET programming, when we use an object that does not exist, the system will throw this exception: system. nullreferenceexception: the object reference is not set to the instance of the object. The following describes several common occasions:
1. If no parameters are passed to the current page, they are directly obtained in the program.
If we do not pass a parameter to the page, assume the parameter is named name, but we directly use request in the program. if querystring ["name"] is obtained, an exception occurs when the object reference is not set to the instance of the object. For example:
String name = request. querystring ["name"]. tostring ();
To avoid such exceptions, You can first determine whether the parameter is null:
If (request. querystring ["name"]! = NULL)
{String name = request. querystring ["name"]. tostring ();}
2. Use sessions, viewstate, and other built-in objects directly when they are undefined.
When no session is defined, viewstate is directly referenced. For example:
String name = session ["name"]. tostring ();
In this case, the exception "the object reference is not set to the instance of the object" is also thrown. To solve this problem, you can first define the value, for example:
Session ["name"] = "ABC ";
Viewstate ["name"] = "ABC ";
Then, if you execute string name = session ["name"]. tostring () Again, no error will occur. Of course, it is also necessary to judge whether the parameter is null before reference:
If (session ["name"]! = NULL)
{String name = session ["name"]. tostring ();}
3. If a value does not exist in the drop-down list, the value is set to the selected status.
This. DDL. Items. findbyvalue ("ABC"). Selected = true;
However, if "ABC" does not exist in the project in the drop-down list, an exception occurs, to solve this problem, you must first determine whether this item exists in the drop-down list:
For (INT I = 0; I <this. DDL. Items. Count; I ++)
{
If (this. DDL. items [I]. value. Equals ("ABC "))
{This. DDL. items [I]. Selected = true; break ;}
}
4. There is no table in the dataset of the record set but it is directly referenced
This. gridview1.datasource = dataset1.tables [0]. defaultview is often used when we bind a record set to the gridview;
However, when dataset1 does not have a data table at all, it will inevitably lead to an exception that "the object is not referenced to the instance of the object". To solve this problem, determine whether the table exists before binding:
If (dataset1.tables. Count> 0)
{This. gridview1.datasource = dataset1.tables [0]. defaultview ;}
5. The control is not defined on the page, but is directly referenced in the program.
This is rare, but it happens occasionally. It is usually caused by incorrect operations when the page is modified. For example, in the previous example, if we accidentally delete gridview1, execute
If (dataset1.tables. Count> 0)
{This. gridview1.datasource = dataset1.tables [0]. defaultview ;}
An exception will be thrown "the object reference is not set to the instance of the object". Add the gridview1 control again.