This is a guest post by Albin Sunnanbo sharing how a mysterious Visual Studio problem made the Set Startup project command fail.
In one of our projects at work we had this mysterious problem with Visual Studio, both 2012 and 2013. It happened to some developers and some machines. When I got a new computer a while ago I was hit with this dreaded bug too. This is a story about how to use available clues to succeed with solving even the strangest problems.
When trying to set multiple startup projects in Visual Studio we right click solution, click Set StartUp Projects…
and expect the familiar dialog to pop up. But no, the menu disappears and then nothing happens. No dialog. Nothing.
If we click Properties instead we get an ugly dialog with the text “Object reference not set to an instance of an object.”
That poor dialog is pretty famous, it got something like a quarter of a million hits on Google. More than I’ll ever get.
Nothing on the top five Google pages seemed to help. We finally found a workaround. Uninstall NuGet package manager, set your startup projects, reinstall NuGet package manager. Not particularly elegant, but at least we could proceed with our regular work.
Clue number one
After a while we realized that it was only our solution that failed, if you opened another project the menu worked fine. Obviously the problem was not cemented in the Visual Studio installation, but triggered by one artifact in our specific project.
We long suspected a buggy Visual Studio extension, but since other projects worked with the same extensions loaded the extensions could be ruled out in this case.
Clue number two
Based on clue number one there must be something specific in our project that triggered the bug. One such candidate was our referenced NuGet packages. NuGet packages can load magic stuff into Visual Studio when a project is loaded. A buggy NuGet package could break all sorts of things, exactly the behavior we experienced.
In hope of an updated NuGet package that would solve our problem we opened Manage NuGet Packages for Solution and expanded the Updates menu.
Clue number three
There was this T4Scaffolding package (among other but this caught my attention). When clicking on the Update button we got this strange dialog
The T4Scaffolding was not found in any of our projects. Any attempts to uninstall it failed too, since it was not in use anywhere.
How come it is there and is not there at the same time? If it is not in any of the projects, where does it come from?
Clue number four
Since we couldn’t find any references at all we deleted the T4Scaffolding.0.9.9 folder from source control and from the working copy, partly because of an old habit of removing things no longer in use, partly because it behaved a bit fishy.
When we restarted Visual Studio and loaded our usual project the Set StartUp Projects suddenly worked again. Rock solid, as if it had been working all the time.
I managed to reproduce this behavior in Visual Studio 2013 with a simple command line project where the T4Scaffolding.0.9.9 folder was copied in from another project.
It does not reproduce with the latest T4Scaffolding left unreferenced in the file tree.
If you too get the famous “Object reference not set to an instance of an object.” dialog, check for unused packages left behind in the packages folder. Remove them and hope for the best.