Clicking and dragging a custom control in VB.NET generates an object reference error message

Failed to create User Control 1 component. an error message appears:

'System.NullReferenceException: Object reference not set to object instance. at System.ComponentModel.ReflectPropertyDescriptor.SetValue (object component, object value) .............. etc ...........

What should I do to fix this error?

+1


source to share


4 answers


When the custom control fails to load into the Visual Studio designer, this is what you need to do. These instructions are for a vb.net project, but C # should be similar. Also, before doing this, close all open windows (or at least the source and design files of the control you are working on.)

Last thing. The FIRST thing you need to do is make sure restarting visual studio doesn't fix the problem. If not, you can try the next steps. These instructions assume the faulty custom controls are in a control library project in visual studio. If not, you should be able to adjust the direction a little to make it work, but much easier when the control is in its own project.

Follow these steps:

  • Make a library to control your startup project.
  • Open the project properties of the management library and click the debug tab.
  • Under Action, click the Run External Program button and select the Visual Studio executable.

NOTE. This means that when you run your solution, it will launch another instance of Visual Studio instead of actually launching your solution. The first instance of Visual Studion (INSTANCE_1) will "host" the second instance of Visual Studion (INSTANCE_2) when it starts.

  1. Run the solution. INSTANCE_2 will load.
  2. Return to INSTANCE_1.
  3. In INSTANCE_1, press CTRL-ALT-E. The Exceptions dialog box opens. Select the Include THROWN column next to Common Language runtime exceptions.

NOTE. This ensures that INSTANCE_1 will BREAK on ANY runtime error, even if it is caught in a try block.



  1. Switch to INSTANCE_2. In Solution Explorer, double click to open a weird custom control.

You should find that INSTANCE_1 from Visual Studio had to stop at the line of code that caused the designer to not load the control. Correct the code (which usually means testing IsNot Nothing before object property references ... but might mean other things.)

Also, sometimes I find that the control is loading in INSTANCE_2 instead of aborting the error in INSTANCE_1. In this case, just stop debugging ... close INSTANCE_2. Save / restart INSTANCE_1 and your problem will go away often.

The lesson is this. The user control MUST be able to load / reference all objects and their elements in order to load them into the constructor. Therefore, for user controls that will be hosted in other containers, I usually raise events to notify the parent rather than trying to drag objects into the child control.

Hope this helps in the future to refer to this old question.

Set

+8


source


Instead of an error when opening a form for editing, it looks like it happens when you are already editing the form and adding new custom controls. A CodeProject article that was mentioned earlier shows what to do in case of incorrect loading of a form, rather than a specific custom control.

Does your user control have any properties that map to custom objects (i.e. not Integer

or String

)? If so, the form designer will try to load your properties into the Property Editor. If an error is thrown while displaying properties, the form designer will show you this. I think this is what is happening with your user control.



If you can edit the question and add more information about the nature of the error (more text about the error), it will help others to help you better. Also, see if you can find the property that might cause the error (for example, if any property depends on a given non-zero value). You can also check out this MSDN article to learn how to restrict control to runtime only.

+2


source


Thanks Seth for this post! Your solution helped me fix this error. The only thing I would like to add to this is that when "INSTANCE_2" is loaded, you may need to actually load the project file into INSTANCE_2. I needed it because I didn't deal with a control library, but instead had custom user controls defined in the same project where the forms were located. After I uploaded the project to INSTANCE_2, I opened an error form and made INSTANCE_1 pop up to the offending code line in User Control.

0


source


I found that this error often occurs when the audit dlls are not building as expected. In 95% of cases, this is fixed by simply restarting Visual Studio.

0


source







All Articles