Duplicate definitions may result in an error in setting the object reference to an instance.

Source: Internet
Author: User

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.
 
 

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.