Duplicate definitions may result in an error in setting the object reference to an instance.
Author: icech
Generally, in C #, the error "the instance where the object is not referenced" is that the control name does not correspond to that in codebehind. For Beginners, repeated definitions in encoding can also cause this problem.
The following is an example:
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Namespace Weste
{
Public class study: system. Web. UI. Page
{
Public String [] C;
Private void page_load (Object sender, system. eventargs E)
{
AAA ();
Bbb ();
}
Private void AAA ()
{
String [] C = new string [3];
// C = new string [3];
C [0] = "we are ";
C [1] = "Western network ";
C [2] = "learning C #";
}
Private void BBB ()
{
Response. Write (C [0] + C [1] + C [2]);
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
Initializecomponent ();
Base. oninit (E );
}
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
}
}
After running, we will find that the following errors are generated:
Exception details: system. nullreferenceexception: object reference is not set to the instance of the object.
Source error:
Row 33: Private void BBB ()
Row 34 :{
Row 35: Response. Write (C [0] + C [1] + C [2]);
Row 36 :}
Row 37:
Many beginners may have questions: I have defined the Public String [] C variable and instantiated string [] C = new string [3]. but why does the error "the object reference is not set to the instance of the object" still be reported when array C is called?
The problem lies in string [] C = new string [3]. After array C is defined, the array is defined again in AAA, the previously defined array cannot be instantiated. You only need to change string [] C = new string [3] to C = new string [3.