Click here to Skip to main content
15,169,285 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
When I click the tab then the error message popup as follows:

The control System.Windows.Forms.TabControl has thrown an unhandled exception in the designer and has been disabled.

Object reference not set to an instance of an object.

Stack trace:
System.Windows.Forms.TabControl.UpdateTabSelection(Boolean updateFocus)
System.Windows.Forms.TabControl.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.TabControl.WmSelChange()
at System.Windows.Forms.TabControl.Wndproc(Message& m)

only two tabs

            // tabControlServings
            this.tabControlServings.Appearance = System.Windows.Forms.TabAppearance.FlatButtons;
            this.tabControlServings.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tabControlServings.Font = new System.Drawing.Font("Calibri", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.tabControlServings.ItemSize = new System.Drawing.Size(120, 40);
            this.tabControlServings.Location = new System.Drawing.Point(0, 0);
            this.tabControlServings.Name = "tabControlServings";
            this.tabControlServings.SelectedIndex = 0;
            this.tabControlServings.Size = new System.Drawing.Size(409, 567);
            this.tabControlServings.SizeMode = System.Windows.Forms.TabSizeMode.Fixed;
            this.tabControlServings.TabIndex = 11;
// tabPageServingType
this.tabPageServingType.Location = new System.Drawing.Point(4, 44);
this.tabPageServingType.Name = "tabPageServingType";
this.tabPageServingType.Padding = new System.Windows.Forms.Padding(3);
this.tabPageServingType.Size = new System.Drawing.Size(401, 519);
this.tabPageServingType.TabIndex = 1;
this.tabPageServingType.Text = "Serving Type";
this.tabPageServingType.UseVisualStyleBackColor = true;

It build success, the error appears just when clicking the tab in the designer view.
F5 and debugging there is no error appeared, I don't know why.
Updated 1-Aug-12 17:43pm
Christian Graus 31-Jul-12 23:56pm
So, your code, which is being executed, has a null object in it. Did you consider telling us the code, or did you not want help ?
chenghuichao 1-Aug-12 0:11am
this comment is moved to question
Christian Graus 1-Aug-12 0:12am
The design view is rubbish. It doesn't run all your code, only some of it. If it works fine when you run it, if you need it to run fine in the designer, run another instance of VS, attach it to the first, and it will catch the exception and you can work out what is happening, and what code you need to add, to make the designer work.

1 solution

You are showing unrelated code. No, don't show any code right now, you don't really understand what your own code does. You sample is unrelated, because the exception is in System.Windows.Forms:[^].

And you did not show any code around this call shown in your stack trace. You see, you exception is one of the easiest to fix. Run it under debugger. To start with, locate your call to the method shown above and put a breakpoint on this line. Try to run. Before the call, look at the members and variables involved. At least one of them is null but is not supposed to be null. Inspect all relevant value under debugger before a call and find out the bug. Apply this approach to other cases. Fix the bug: either make sure the problematic reference variable or member is initialized with a non-null reference (constructor called or your reference is assigned to some already initialized variable), or, if appropriate, make a conditional statement: check a variable or a member in question for null and don't the operation where it is used if it is null.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900