Object reference is not set to the instance of the object -- Summary of possible problems

Source: Internet
Author: User

I. general statement on the network

1. The viewstate object is null.
2. dateset is empty.
3. datareader is empty due to SQL statements or datebase.
4. When a string variable is declared without a null value, the variable is applied.
5. The new initialization object is not used.
6. The session object is empty.
7. When you assign a text value to the control, the value does not exist.
8. When request. querystring () is used, the obtained object does not exist, or the initial value is not assigned when the value is null.
9. When findcontrol is used, the control does not exist but is not preprocessed.
10. Repeated definitions may result in an error in setting the object reference to an instance.

 

 

II,

(1) If the set variable is null or the value is not obtained, this problem usually occurs when the number of arguments is passed, it also appears when you use data controls such as DataGrid, gridview, and datalist.

(2) The Control name does not match that in codebehind.

(3) The new initialization object is not used.

(4) The control referenced in the program does not exist.

Solution:

(1) Use try... catch... finally to catch errors, or directly use response. Write () to output the value of the variable.

(2) check whether there are uninitialized variables in the code

 

 

III,

Sqlconnection. Open has not set the object reference to the instance of the object.

When using vs2003 to Develop ASP. NET programs, sometimes the open () method of the sqlconnection object appears.

You have not set the object reference to the instance of the object.

Note: An unhandled exception occurs during the running of the current Web request. Check the stack trace information for details about the error and the source of the error in the code.

Exception details: system. nullreferenceexception: object reference is not set to the instance of the object.

Source error:
....

Conn. open ();

......

Tracking and debugging are sure to be able to perform new operations on the conn, but the program often reports errors in this place. However, sometimes it is possible to restart the server or restart IIS. Why can't I find the problem? In this case, the. NET frameework framework installed on your local machine may be faulty. The sp1.1 patch may not be installed.
Install the net frameework1.1 SP1 Patch on the Microsoft official site.

 

 

IV,

In general, if an nullreferenceexception occurs, I personally summarize the following situations:
1. The namespace of the object is not referenced.
2. The object is not instantiated.
3. If an exception occurs, the instantiation failure object is null.

 

 

V,

When IIS is unable to handle the problem, the above error occurs.
An error occurs when the user question count of an application exceeds the limit.
Improve your program, use the cache, and minimize the number of interactions with the database.

 

 

VI,

The problem I encountered was that I had no intention to reset the dataset reference. This problem occurs later. Please check your code and see if it was referenced again elsewhere, in my program, dataset is set as a global object.

 

 

VII,

When you find that all methods are not running, install the. netframework patch.

 

 

8,

This error occurs only because the Dal DLL file in the site is invalid and the business layer cannot instantiate the objects in idal.
But it also wasted one afternoon on code calling.

 

 

IX,

Add monitoring to every suspicious location. At first, I thought it was an SQL statement error. I checked it several times and used the SQL statement to report the same error. According to the results from Google, the mention of object redefinition will cause this result. Based on this prompt, the page-level global object is replaced.
Class dboper {
/* The constructor does not have a function body.
*
* C # Write a series of Database Operations
*
*
}
Initially, to save time and minimize object declaration, a private dboper DB = new dboper () is declared globally and the method of the object is called in different methods, to avoid Memory waste caused by definition. After code reconstruction, no error was reported during compilation. However, during execution, "nullreferenceexception: the object reference is not set to the instance of the object" appears. As a result, it took several hours. Later, based on Google's results, the dboper objects used in different methods were redefined as local variables one by one (only one dboper object was defined in a method, and the method was normally called multiple times ), remove the global private variable. The execution will be normal.
As to why this problem occurs when the defined object is put into class as private and used in different methods, it is not very clear now .........................

Object reference is not set to the instance of the object -- Summary of possible problems

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.