Cannot add a MVC controller VS2012 gives "object reference not set to an instance of an object"

17,467

Solution 1

The issue seems to have been caused by an incomplete install/registration of the MvcScaffolder package. The following process resolved my issue for me.

Start Visual Studio and open the project. Open the Package Manager Console. (This was the "trick" or missing piece of information that led to the solution...)

Here I saw the following error caused by the PowerShell script used for registration:

New-Object : Could not load file or assembly 'System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
At <ProjectPath...>\packages\MvcScaffolding.1.0.9\tools\registerWithMvcTooling.ps1:163 char:27
+     $newProvider = New-Object <<<<  $powerShellScaffolderProviderType($mvcScaffoldingProvider)
    + CategoryInfo          : NotSpecified: (:) [New-Object], FileNotFoundException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException, Microsoft.PowerShell.Commands.NewObjectCommand

When I checked the registered scaffolders from the Package Manager Console I got:

PM> get-defaultscaffolder

DefaultName          ScaffolderName
Action               MvcScaffolding.Action
Controller           MvcScaffolding.Controller
CustomScaffolder     T4Scaffolding.CustomScaffolder
CustomTemplate       T4Scaffolding.CustomTemplate
DbContext            T4Scaffolding.EFDbContext
Mailer               Mailer.Razor
Repository           T4Scaffolding.EFRepository
UnitTest             MvcScaffolding.ActionUnitTest
View                 MvcScaffolding.RazorView
Views                MvcScaffolding.Views 

So clearly the scaffolders are configured to use the MvcScaffolding package, but the package had a registration error.

A quick search for the error provided this link http://mvcscaffolding.codeplex.com/discussions/437692 which refers to https://visioautomation.codeplex.com/discussions/397034 that suggests installing PowerShell 3, which is actually part of the Windows Management Framework 3.0, available from http://www.microsoft.com/en-us/download/details.aspx?id=34595. I picked the Windows6.1-KB2506143-x64.msu file when prompted because I’m running Win 7-64-bit.

Restart the PC after installation, re-open Visual Studio and the project and look at the Package Manager Console. If the error is gone, then so is the controller problem.

Solution 2

I had this problem on VS2012 on Win8, We where working on a project that we started developing on VS2010 and i wasn't able to create new controllers with the CTRL+M, CTRL+C shortcut.

Opening any project and re-installing MvcScaffolder and its dependencies under Manage Nuget package have fixed this for me.

Didn't have to restart VS2012 at all.

Solution 3

Maybe your existing project have wrong ProjectTypeGuids element in .csproj file? Right one for MVC4 is {E3E379DF-F4C6-4180-9B81-6769533ABE47}.

Try to compare what project type do you have in new project and in your existing project.

Solution 4

The preferred answer of installing the Windows Management Framework 3.0 did not work. The download "Windows6.1-KB2506143-x86.msu" did not work on my Windows 7 32 bit system. It failed with the error "The update is not applicable to your computer".

Reinstalling MVCscaffolding with Manage Nuget Packages did not work.

My ProjectType GUID was correct.

Microsoft Web Developer Tools was already installed.

Deleting and recreating the solution didn't work.

It was necessary to repair the Visual Studio 2012 installation from the install disk to fix this issue.

Share:
17,467
Francois Grobler
Author by

Francois Grobler

I am an engineer specialising in Engineering Information Systems and tools in my capacity as Chief Architect and Technical Director for GESS (Pty) Ltd.

Updated on July 31, 2022

Comments

  • Francois Grobler
    Francois Grobler almost 2 years

    I have searched extensively and cannot find another person on the planet with this problem...
    The symptoms are:
    In Visual Studio 2012, on a MVC 4.0 Project that was upgraded from VS2010 and is configured for dual version editing from VS2012 and VS2010. When I right-click on the Controllers Folder and select Add->Controller... from the context menu, I get an "Object reference not set to an instance of an object" error from Visual Studio.

    The existing code in the project runs fine, and if I use the Add->New Item option instead, and pick the Controller from the dialog box, then I can add a blank controller. I just cannot add a typed controller using the method above.

    If I however create a new MVC4 Project, then I can add typed controllers again using the same menu, so the problem does not exist in Visual Studio's installation, it has to exist in the Project File.
    I have also checked the references between my project and a new MVC Project and all the new project references are included in my project.
    Has anyone experienced something similar?

    How do I fix this?

  • Francois Grobler
    Francois Grobler about 11 years
    I have 3 project type guids: {E3E379DF-F4C6-4180-9B81-6769533ABE47}; {349c5851-65df-11da-9384-00065b846f21}; and {fae04ec0-301f-11d3-bf4b-00c04f79efbc}
  • Mark
    Mark about 11 years
    Hi - I'm having EXACTLY the same problem - did you find an answer?
  • Francois Grobler
    Francois Grobler about 11 years
    I have. I'm just writing up the answer and will post it in a few minutes.
  • MCM
    MCM almost 11 years
    Thanks! exactly what I needed.