views:

443

answers:

2

I'm getting this error on a compact framework form.

"code generation for property 'inputControl' failed. Error was: 'object reference not set to an instance of an object.'"

Anyone have any idea why? It just started and was never an issue before.

+1  A: 

Take a look at where 'inputControl' is intiially being accessed and ensure it's being instantiated correctly.

Whytespot
Ya I just figured out it was on a custom user control and it is. Like I said it just started happening. So I guess somewhere it isn't being instantiated right...or just never gave me an error.
nportelli
+1  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