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

  • Question

  • 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


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

    • Edited by dev-zhang Friday, June 14, 2013 3:50 AM
    Friday, June 14, 2013 3:48 AM

All replies

  • Perhaps this.Controls is null. You haven't accidentally hidden Form.Controls by creating your own property with the same name, by any chance?
    Friday, June 14, 2013 4:48 AM

    Thank you for your reply.  

    I have tried to add code to check this.controls before add control to it.  it's not null.

    and this error only happens on few 64bit machines.   That's why it's confusing.   I can't reproduce this error on my side.  and can't tell what's the differences between the machine which can run correctly and the machine which can't.

    Friday, June 14, 2013 6:32 AM