Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

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

$
0
0

Hi,

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)

I also found Microsoft .NET Framework Repair Toolafter some search on web.   and send it to my user.

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 .Net Framework? Did anyone meet this error on Windows 7 64bit?

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


Here are the codes related to the user control, all these codes is generated from the Forms Designer:

all these codes run correcly on most 64bit machines.    

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);



Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>