SoFunction
Updated on 2025-03-09

Run prompt not setting object reference to object instance error solution

An instance of an object reference not set to an object
1. General statements on the Internet
1. The ViewState object is Null.
2. DateSet is empty.
3. DataReader is empty due to sql statements or Datebase.
4. When declaring a string variable, the variable is applied without assigning a null value.
5. The object is not initialized with new.
6. The Session object is empty.
7. When assigning text values ​​to the control, the value does not exist.
8. When using (), the acquired object does not exist, or the initial value is not assigned when the value is empty.
9. When using FindControl, the control does not exist but is not pre-processed.
10. Repeated definitions cause an instance error in which the object reference is not set to the object.
two,
(1) The set variable is empty or has not obtained a value. This problem usually occurs when passing parameters, and it will also occur when using data controls such as DataGrid, gridview or datalist.
(2) The control name does not correspond to the codebehind in codebehind
(3) The object is not initialized with new
(4) The control referenced in the program does not exist
Solution:
(1) Use try..catch... finally to catch the error, or directly use () to output the variable value obtained
(2) Check whether there are uninitialized variables in the code
3. The object reference is not set to an instance of the object
When using VS2003 to develop programs, sometimes it will appear when operating the Open() method of the SqlConnection object
An object reference is not set to an instance of the object.
Description: An unhandled exception occurred during the execution of the current web request. Please check the stack trace information for details about the error and where the error was caused in the code.
Exception details:: Object reference is not set to an instance of the object.
Source error:
。。。。
();
......
Tracking and debugging can definitely confirm that new operation is performed on conn, but the program often reports errors in this place, but sometimes restarting the server or restarting IIS can be used normally. No matter how you debug, you can't find the problem. At this time, there may be a problem with the .NETFRAMEEWORK framework installed on your machine, and there may be no SP1.1 patch installed.
You need to call NET to reprint it from Baifen.com http://. Please keep the patch marked FRAMEEWORK1.1SP1 and download and install it on Microsoft's official website.
4. I personally summarize the following situations for the NullReferenceException exception:
1. The namespace where the object is located has no reference
2. Object is not instantiated
3. An exception occurs, and the instantiation failed object is null
5. When IIS cannot withstand it, the above errors will also occur.
An error occurs when the application's user visits exceed the range it can withstand.
Improve your program, use cache, and minimize the number of interactions with the database.
6. The problem I encountered was that the DataSet reference was accidentally reset. This problem occurred later. Please check your code carefully. Whether it was re-referenced elsewhere. In my program, DataSet was set to a global object.
7. When you find that all the methods are not working, put on the .netframework patch
8. I have this error because the DAL dll file in the website is invalid, and the business layer cannot instantiate the objects in the IDAL
But I wasted all afternoon on the code adjustment
Nine,
I added monitoring for every suspicious place. At first I thought it was written incorrectly by the SQL statement. I checked it several times, but I still reported the same error when I changed to the SQL statement. According to the results from Google, mentioning object redefinition will cause such a result. According to this prompt, replacing the page-level global object is finally done.
classDBOper{
/*Constructor has no function body
A series of database operation methods written by **c#
**}Initially, in order to save trouble, I tried to declare the object as little as possible, and only declared a privateDBOperdb=newDBOper() globally, and called the method of the object in different methods to avoid memory waste for it definition. Later, the code was refactored and there was no error when compiling. However, when running, "NullReferenceException: The object reference is not set to the instance of the object" appears. It ended up being done for several hours because of it. 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 is defined in one method, and it is normal to call its method multiple times), and the global private variable was removed. It will be normal when it runs again.
As for why such a problem occurs after applying a custom object to a class as private and using it in different methods, it is not very clear yet.