Nancy Street

Designer Exception

Click to see the Site Map
HomeInfoMusicGalleryPetsGeoHobbiesGeo
Site MapWhat's NewRecent ChangesContactsServer StatisticsSite Information Home « Computers « DevBlog

Back to: Development Blog Contents

An Exception when viewing a form in the Visual Studio designer wastes some time, but a neat solution is revealed.

While writing a .NET Forms application with a complex structure of controls I somehow caused the Visual Studio designer to crash with a null reference Exception when viewing the form in the designer. Instead of seeing my form contents I received a large white box full of red text and a stack trace over the top of the control.

Only by tediously commenting out code incrementally did I eventually stumble across the offending call in the OnLoad method. I was using a reference that was previously passed in via the constructor. At runtime this was fine of course, but at design time I presume the default constructor for the class was being used and my references were null.

The cure was simply to put a if (thing == null) check around the reference, but the hard part was finding the point of the Exception, as the debugger is alive when using the designer.

The formal answer arrived thanks to David in the stanski .NET developers discussion group:

From: dotnet-owner@stanski.com [mailto:dotnet-owner@stanski.com] On Behalf Of David K
Sent: Wednesday, 17 March 2004 13:54
To: 'dotnet@stanski.com'
Subject: RE: [aus-dotnet] NullReferenceException in a UserControl in Designer

Start another instance of Visual Studio with your control project, attach to the another Visual Studio instance (Debug -> Processes...), setup the Visual Studio to break into the debugger the when a NullReferenceException is thrown (Debug -> Exceptions) and load the page with your control in the designer, and it should break into the second instance.

This advice is spot-on. I created an empty project for the 2nd instance of studio to run in, attached the process, set it to break on Exception throw and it immediately trapped the error in the 1st studio instance process and showed me the exact offending source line. It's just "luck" I guess that I've never needed to do this type of debugging before, so I overlooked the facility completely.

Back to: Development Blog Contents


Contact Information | PGP Keys | Site Map | What's New | Visitor Book
Last Updated: 06-Aug-2007 21:10
Copyright © 1999-2007 Orthogonal Programming