简体   繁体   中英

System.NullReferenceException on this.Controls.Add() with winforms and C#

I have developed a small WinForm application with C#. I have released this app for years, and a lot of users use it on many different Windows system, Windows XP, Vista, 7, 8, 32bit and 64bit.

But a few days ago one user reported a crash when launching application on a Windows 7 64bit machine. Because I can't reproduce the error on my side so I sent him the debug version which can output log information.

Now I have located the line which fails on

this.Controls.Add(this.myUserControl);

It's a line which Visual Studio generated from the Forms Designer. "this.myUserControl" is an instance of an User Control. This User Control is built in this project, and not includes any external components. Just built with some standard UI controls. The .NET error information is

"System.NullReferenceException: Object reference not set to an instance of an object."

I have added code to test whether the this.myUserControl is null before adding it to controls, it's definitely not null.

I have tried a lot of methods, like using corflags.exe to force it running in 32bit mode or changing the build target framework to 3.5 (before it's 2.0)

Nothing changes. It always crashes on that line.

I have tested this app on Windows 7 64bit before I released it, and this app have a lot of users using it on Windows 7 64bit. So I think I should not change any code. But I don't have any direction on how to solve this problem.

Should I tell the user to reinstall Windows or repair .Net Framework? Did anyone meet this error on Windows 7 64bit?

English is not my native language so sorry for my English.


The code related to the user control, all these codes is generated from the Forms Designer:

private MyUserControl myUserControl;

this.myUserControl = new ProjectNameSpace.MyUserControl();
this.myUserControl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.myUserControl.Location = new System.Drawing.Point(12, 80);
this.myUserControl.Name = "myUserControl";
this.myUserControl.Size = new System.Drawing.Size(775, 412);
this.myUserControl.TabIndex = 2;
this.myUserControl.Resize += new System.EventHandler(this.myUserControl_Resize);

this.Controls.Add(this.myUserControl);

As you stated, this.myUserControl is not null; so this.Controls might be the issue. Check for System.ComponentModel.IContainer components and InitializeComponent() ; there possibly be something lack or not initialized.

Override OnLayout or OnPaint would be helpful for debugging, IsHandleCreated could also be a key point for figuring out something abnormal.

The simplest way would be create a new user control with designer, and paste the code you've written for the business. Remember to name the controls as same as the original.

As for the stack/line of the crash; so you are saying this line in question in your Designer code is at the very top of the stack, even if you have "Show External Code" checked?

Although you have confirmed it is crashing on the line in question:

this.Controls.Add(this.myUserControl);

I am a bit suspicious. If it is crashing on that line, and not inside of the Controls.Add method, then it seems like there are only two possibilities:

1) this.Controls is null - have any controls been added before this one? If so, it seems impossible.

2) this is null - which is impossible..

One thing you could try is sending this customer code that is just a simple for, adding a simple control (that is not your custom control). Or have them run some other C# forms app. That may help you narrow things down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM