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


i have developed small winform application c#. have released app years, , lot of users use on many different windows system, windows xp, vista, 7, 8, 32bit , 64bit.

but few days ago 1 user reported crash when launching application on windows 7 64bit machine. because can't reproduce error on side sent him debug version can output log information.

now have located line fails on

this.controls.add(this.myusercontrol); 

it's line visual studio generated forms designer. "this.myusercontrol" instance of user control. user control built in project, , not includes external components. built standard ui controls. .net error information

"system.nullreferenceexception: object reference not set instance of object."

i have added code test whether this.myusercontrol null before adding controls, it's not null.

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

nothing changes. crashes on line.

i have tested app on windows 7 64bit before released it, , app have lot of users using on windows 7 64bit. think should not change code. don't have direction on how solve problem.

should tell user reinstall windows or repair .net framework? did meet error on windows 7 64bit?

english not native language sorry english.


the code related user control, these codes generated 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 stated, this.myusercontrol not null; this.controls might issue. check system.componentmodel.icontainer components , initializecomponent(); there possibly lack or not initialized.

override onlayout or onpaint helpful debugging, ishandlecreated key point figuring out abnormal.

the simplest way create new user control designer, , paste code you've written business. remember name controls same original.


Comments