locked
Attempt to add new controller generates "Object Reference not set to instance of object" error. RRS feed

  • Question

  • User-1842880510 posted

    I have a project that I have been working on for some months.  I am beginning a new section of the project so I go to add a controller using the Controllers context menu item "Add/Controller" like I've done several times before and I get a message box saying "Object Reference not set to instance of object".  I recall seeing this happen before, but I currently can't find the post that resolved this problem.

    Has anyone seen this before?

    Thursday, May 22, 2014 3:27 PM

Answers

All replies

  • User-1657171777 posted

    Can you post the code where this is occurring?

    Thursday, May 22, 2014 3:41 PM
  • User1957004874 posted

    Are you using scaffolding, it could fail because of that, Also look for scaffolding package, incomplete installation can cause this error. you need to reinstall it.

    Thursday, May 22, 2014 4:13 PM
  • User281315223 posted

    There are a variety of issues that could be related to this, however without a bit more detail it might be tough to determine exactly what might be going on.

    Are you receiving the error as soon as the "Add Controller" or "Add View" dialog appears? Or does it occur after you attempt to select a model type to populate the new View itself (if this is the case it could be related to the built-in scaffolding in MVC).

    Any additional information that you could provide might be extremely helpful.

    Thursday, May 22, 2014 4:20 PM
  • User-1842880510 posted

    Are you receiving the error as soon as the "Add Controller" or "Add View" dialog appears?

    No it never gets that far.  You select "Add" then select "Controller", then the "Object Reference not set to instance of Object" message box opens.

    Thursday, May 22, 2014 4:25 PM
  • User281315223 posted

    Have you tried restarting Visual Studio?

    Does this only occur in a single project / solution? Have you tried creating another "sample" project to see if it occurs there as well?

    Thursday, May 22, 2014 4:27 PM
  • User-1842880510 posted

    Are you using scaffolding, it could fail because of that, Also look for scaffolding package

    Scaffolding has always been used to create previous controllers and views.  (There were some issues on a different machine at one time where the scaffolded views did not contain any of the helpers for the model, but not on this machine.

    Thursday, May 22, 2014 4:28 PM
  • User-1842880510 posted

    Have you tried restarting Visual Studio?

    Yes

    Does this only occur in a single project / solution?

    No It occurs in my only other MVC project.

    Have you tried creating another "sample" project to see if it occurs there as well?

    OK I just did that.  I created a MVC4 project from scratch and then tried Add\Controller and the add controller dialog opened.

     

    Thursday, May 22, 2014 4:35 PM
  • User-1842880510 posted

    Well the long weekend is over.  Does anyone have any suggestions?

    Tuesday, May 27, 2014 9:32 AM
  • User2054462546 posted

    me too

    Wednesday, May 28, 2014 11:27 PM
  • User281315223 posted

    Can you supply a bit more details about the project?

    You mentioned that you recently added a new "section" to it and then began experiencing this issue. Any additional details that you could provide might be helpful as this could be an issue with how the actual solution / project is configured or perhaps within Visual Studio itself (which is less likely as you were able to do this same thing within another project).

    Thursday, May 29, 2014 8:05 AM
  • User-1842880510 posted

    I have a HomeController whose Index page controls the main page of the website which is essentially the out of the box MVC 4 project Home page created by VS 2012.

    I needed to create a section that creates and edits something  we call "Requirements"  So I created a "RequirementsController" that controlled that.  I also added a Requirements Folder to the views folder.

    Then I did some work on the section that creates and edits "Funding Documents"  So I created a "FundDocsController" that controlled that, and added a "FundDocs" folder to the views folder.

    The controllers are all within the "Controllers" folder.  The "Edit",  "Create", "Details", "Delete", and "Index" views for each controller are contained withing the corresponding folders under the views folder.  The models are all contained within the Models Folder.  (The project implements an EF representation of a database with stored procedures with specific view models create that reside directly under the Models folder.  All scripts reside in the "Scripts" folder.  All styles reside in the "Content" folder.  All images reside in the Images folder.  Essentially an unimaginative adaptation of the default out of the box configuration of an MVC 4 EF 5 database first application.

     

    Now I need to work on the section that creates and edits "Purchase Requests".  So I need to create a "PRController" that controls that, and add a PR folder to th views folder.  But the PRController will not create.

     

    Friday, May 30, 2014 11:26 AM
  • User-1842880510 posted

    Does anyone else have any suggestions?

    Friday, June 13, 2014 8:52 AM
  • User-1312088494 posted

    Start Visual Studio and open the project. Open the Package Manager Console.

    execute this command there:

    install-package MvcScaffolding

    Sunday, June 15, 2014 4:37 PM
  • User-1842880510 posted

    Start Visual Studio and open the project. Open the Package Manager Console.

    execute this command there:

    install-package MvcScaffolding

    This is the result

    PM> install-package MvcScaffolding
    Attempting to resolve dependency 'T4Scaffolding'.
    Attempting to resolve dependency 'T4Scaffolding.Core'.
    Attempting to resolve dependency 'EntityFramework'.
    You are downloading EntityFramework from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkID=320539. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
    Successfully installed 'EntityFramework 6.1.0'.
    Install failed. Rolling back...
    Install-Package : File contains corrupted data.
    At line:1 char:16
    + install-package <<<<  MvcScaffolding
        + CategoryInfo          : NotSpecified: (:) [Install-Package], FileFormatException
        + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand
     

    Tuesday, June 17, 2014 10:42 AM
  • User1707006414 posted

    This is an old thread but I ran into this issue today.  Happened after my PC was replaced. 

    The fix was to install Windows Management Framework 3. 

    https://www.microsoft.com/en-us/download/details.aspx?id=34595

    I found this info at the below link

    https://stackoverflow.com/questions/16148914/cannot-add-a-mvc-controller-vs2012-gives-object-reference-not-set-to-an-instanc

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 26, 2017 11:58 AM
  • User-1842880510 posted

    I did  resolve that a couple of  years ago, but none of the suggestions made above worked, and I don't remember what I did to resolved this.  (I never posted the fix because at that time we were being blocked from accessing forums.asp.net). 

    No one else has mentioned getting the problem and resolving it.  However, the stackoverflow site you referred to has several people who experienced the issue and resolved it with different methods.  I would advise everyone to read that thread and then deciding which fix to employ, as reinstalling Windows Management Framework 3 is not always an option due to limited rights on the machine given to the person experiencing the issue.

    So You now will get the points.

    Tuesday, September 26, 2017 1:13 PM