Question How to debug when exception is thrown in designer?

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
Most people here have probably seen this error before:

"to prevent possible data loss before loading the designer, the following errors must be resolved"

Currently I'm battling with a situation where I can't seem to find a solution. When I open design view for the form I get the following two exceptions:

1:
Object reference not set to an instance of an object.
at VeryLongBoringPath\LagerList.vb:line 86

2:
The variable 'LagerList1' is either undeclared or was never assigned.
Andresen frmLagerMainForm.Designer.vb Line:289 Column:1


The code at line 86 in LagerList.vb:
VB.NET:
Private userNameHeight As Integer = 15

The code at line 289 in frmLagerMainForm.Designer:
VB.NET:
Me.Controls.Add(Me.LagerList1)


The variable LagerList1 is declared and assigned at that point. Just before this line I have the following code, so at this point LagerList1 IS set to something, otherwise it would fail here, right?
VB.NET:
Me.LagerList1.Text = "LagerList1"


Also note that it is only when I try to view the designer that it fails. When I run the application everything works just perfectly


Is there a way how to debug what happens when I open the designer view?
 
Check your frmLagerMainForm.Designer.vb file for a "Private WithEvents LagerList1 as ...." line. The member declarations are typically at the end of the file. Check your Designer code for the portion where the properties of LagerList1 are set. Make sure they exist and there isn't something funny going on there.

If all else fails, delete all references to LagerList1 from your designer file, rebuild the project, and add LagerList1 again.
 
Sorry to resurrect a zombie thread, but I just solved a similar problem and I figured I'd share, just in case anyone is still paying attention.

My setup was very similar to the OP's, The two exceptions for me were:
1:
Object reference not set to an instance of an object.
at VeryLongBoringPath\PlateContextMenu.vb:line 53
2:
The variable 'platemapContext is either undeclared or was never assigned.
frmFormName.Designer.vb Line:578 Column:1


The code at line 53 (and, as it turns out, line 54) turned out to be the culprit:

VB.NET:
Public SOME_SETTING As String = clsConfig.GetSettingValue("SOME_COOL_SETTING").ToString
Public ANOTHER_SETTING As String = clsConfig.GetSettingValue("SOME_BORING_SETTING").ToString

clsConfig is a class in my project that has methods for getting settings out of the dll.config files for the various class libraries in the project. I discovered that if I got rid of the call to GetSettingValue just in line 53 - I just set SOME_SETTING to String.Empty and commented out the call - and rebuilt, the designer would fail to open again, except this time telling me that line 54 was throwing the "Object ref not set to an instance..." exception. Hmmm...

So I modified line 54 the same as line 53, rebuilt, and lo! there was much rejoicing! The designer loaded (for the first time in years, I might add). So what's happening (I think) is that at design time, VS is not able to instantiate clsConfig, or perhaps it can, but clsConfig throws an exception when it instantiates, possibly because it can't find the dll.config file. At runtime, the dll.config file is found, clsConfig instantiates correctly, and the form loads and runs without any problems.

Of course all I've done here is make this work at design time. Commenting out that code will break things at run time, so I need to find somewhere else to initialize those two member attributes with the settings from the dll.config. But that's my problem, not yours.

Hope this helps someone.

</log>
 
Back
Top