views:

444

answers:

3

Failed to create component 'User Control 1'. the error message follows: 'System.NullReferenceException : Object reference not set to an instance of an object. at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object Component, Object Value) .............. etc..........

please help me......... Thanx in advance......

+1  A: 

Instead of being when you open up a form for editing, it sounds like this is occurring when you are already editing a form and adding new user controls. A CodeProject article that was previously mentioned shows what to do in the case of a form not loading correctly, rather than a specific user control.

Does your user control have any properties that map to custom objects (ie. not Integer or String)? If so, the Form Designer will attempt to load your properties into the Property Editor. If showing the properties generates an error, the Form Designer will show that to you. I think this is what is happening with your user control.

If you could edit the question and add more information about the nature of the error (more of the error text) it would be of better assistance to others in helping you. Alternatively, see if you can find the property that might be causing the error (does any property rely on a non-null value being set?). You can also take a look at this MSDN article for tips on how to limit the control for just run-time.

Tom
Up-voted because your answer is better than mine.
RoadWarrior
A: 

When a User Control won't load into the Visual Studio designer here is what you need to do. These instruction are for vb.net project but c# should be similar. Also, before doing this close all open windows (or at least the source and designer files of the control you are working on.)

One last thing. The FIRST thing you should do is ensure that restarting visual studio doesn't fix the problem. If not you can try the steps that follow. These instructions assume that the errant user controls are in control library project in visual studio. If not you should be able to adjust the directions a bit to get it to work but it is much easier when the control is in its own project.

Do the following:

  1. Make the control library your startup project.
  2. Open the properties for the control library project and click on the debug tab.
  3. Under Start Action click the Start external program option and browse to the Visual Studio executable.

NOTE: what this means is that when you run your solution it will fire up another instance of Visual Studio instead of actually running your solution. The First Instance of Visual Studion (INSTANCE_1) will "host" a second instance of visual studio (INSTANCE_2) when you run it.

  1. Run your solution. INSTANCE_2 will load.
  2. Switch back to INSTANCE_1.
  3. In INSTANCE_1 hit CTRL-ALT-E. This will open up the exceptions dialog box. Check On the THROWN column checkbox next to Common Language Runtime Exceptions.

NOTE: This will ensure that INSTANCE_1 will BREAK at ANY runtime error even if it is hit in a try block.

  1. Switch to INSTANCE_2. In Solution Explorer double-click to open the errant user control.

You should find that INSTANCE_1 OF Visual Studio should have stopped at the line of code that caused the designer to not load the control. Fix the code (which usually means testing for IsNot Nothing before references an object properties...but could mean other things.)

Also, sometimes I find that the control WILL load in INSTANCE_2 instead of breaking on an error in INSTANCE_1. In that case just stop debugging...close INSTANCE_2. Save/Restart INSTANCE_1 and your problem will often have gone away.

The lesson is this. User Control MUST be able to load/reference all objects and their members in order to load it into the designer. So for User Controls that will be placed onto other containers I will usually design events to notify the parent rather than trying to push objects into the child control.

Hope this helps for future reference on this old question.

Seth

Seth Spearman
A: 

Thanks, Seth, for this post! Your solution helped me nail down the error. The only thing I would add to this is that when "INSTANCE_2" loads, you may need to actually load the project file in INSTANCE_2. I needed to because I wasn't dealing with a control library, but instead had custom User Controls defined in the same project that the forms were located in. Once I loaded the project in INSTANCE_2, I opened the erring form and that caused INSTANCE_1 to pop up to the line of offending code in the User Control.

Paul glad it helped. It would be great if you could upvote my answer. Just click the little ^ that is above the 0 in my answer.Seth
Seth Spearman