145

After installing the ASP.NET and Web Tools 2015 (RC1 update 1) I get the following error within ASP.NET MVC 4 projects when opening *.cshtml files:

Object reference not set to instance of an object

Enter image description here

I get the same error message even if I start a new ASP.NET MVC 4 project and try to open a *.cshtml file there.

I'm using Visual Studio 2015

TylerH
  • 20,799
  • 66
  • 75
  • 101
Carsten Cors
  • 2,505
  • 2
  • 14
  • 21

18 Answers18

218

Maybe this helps:

  1. Delete the contents from the following folders:
    1. C:\Users\%userprofile%\AppData\Local\Microsoft\VisualStudio
    2. C:\Users\%userprofile%\AppData\Local\Microsoft\VSCommon
  2. Sometimes, you need to go to:
    1. [x64] C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE
    2. [x86] C:\Program Files\Microsoft Visual Studio 14.0\Common7\IDE

and run devenv /resetuserdata.

Expect, that all of your user settings such as Visual Studio layout, linked Microsoft account or start page might disappear.

Draken
  • 3,134
  • 13
  • 34
  • 54
Fka
  • 6,044
  • 5
  • 42
  • 60
  • where are you running devenv/reset userdata? from the command line at that directory? – dinotom Dec 10 '15 at 16:03
  • Yes. There is such .exe file. – Fka Dec 10 '15 at 16:18
  • 7
    Worked!! However, I just renamed the Visualstudio & VSCommon dirs to VisualStudio_old & VSCommon_old. No need to run "devenv /resetuserdata" – Ray Jan 18 '16 at 21:06
  • In some weird behavior cases of Visual Studio I used to delete the *.suo file from the location where the solution file is. – Damian Kobak Jan 20 '16 at 09:17
  • 1
    @DamianKobak - yes, it often helps, but only for a specific solution. When things go wrong on every project steps in answer suit better. – Fka Jan 20 '16 at 09:19
  • 96
    Actually it is sufficient do delete folder ComponentModelCache on path C:\Users\{user}\AppData\Local\Microsoft\VisualStudio\{version - 14 or 12}. You can keep the rest and no settings get lost... – Tom Burger Feb 23 '16 at 18:03
  • 1
    This is applicable for all VS, because they differ only in `\Microsoft Visual Studio \Common7\IDE`. – Fka Aug 08 '16 at 09:47
  • Okay, I like this answer because I believe it works but running devenv.exe /? does NOT show /resetuserdata as an official command-line switch. Nor does this page, https://msdn.microsoft.com/en-us/library/xee0c8y7.aspx?f=255&MSPPError=-2147217396: – Bradley Dec 06 '16 at 06:03
  • 2
    I can +1 to @Tom Burger´s comment above. And if you don't want to remember the path and file name for next time you can use Mads Kristensens excellent extension Clear Component Cache (https://github.com/madskristensen/ClearComponentCache). – savehansson Dec 07 '16 at 08:31
  • 3
    **Make sure you EXPORT YOUR SETTINGS** as this will wipe them out and you'll have to import them afterward.This just fixed a similar error for me in VS2017 that occurred after I added C++ support through the installer. Note that deleting ComponentModelCache *did not* help. – aremmell Jun 14 '17 at 19:30
  • /resetuserdata is not working in the latest version VS 2019, `devenv /ResetSettings` (without the trailing dot) works. – 7kemZmani Aug 02 '19 at 11:42
  • @7kemZmani FYI the trailing dot is not part of the command but part of the prose of the answer. – TylerH Sep 05 '19 at 18:47
  • Only deleting the cache folders won't work if you messed up path environment variables for msbuild. Then you will have to run `devenv /resetuserdata` too. – PauMAVA Mar 13 '21 at 15:30
  • Ehh, original author is forgotten. Only editor is mentioned :( @Jaggana – Fka Nov 24 '21 at 10:32
79

Delete %LocalAppData%\Microsoft\VisualStudio\14.0\ComponentModelCache and restart Visual Studio.

Alternatively, use the Clear MEF Component Cache extension.

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
17

For me,

  1. I ended the process in Windows Task Manager: VsHub.exe.
  2. Restarted Visual Studio.

After that, everything works like a charm again!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hao
  • 266
  • 1
  • 12
  • I upvoted your answer because it is solve the problem of 'Object Null Reference' but after that i got new exception in Visual Studio `The operation is not completed, invalid pointer` – Hakan Fıstık May 16 '16 at 14:32
8

I solved it doing

run devenv /resetuserdata

in this path:

[x64] C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE

I assume that in x86 it works in this path:

[x86] C:\Program Files\Microsoft Visual Studio 14.0\Common7\IDE
TylerH
  • 20,799
  • 66
  • 75
  • 101
5

It's recommended to try any or all of the following:

  • Restart Visual Studio

  • Try Running As Administrator (right-click Visual Studio and choose "Run As Administrator")

  • Check for any updates for Visual Studio (download and install them if any are available)

  • Try opening a different solution / project

If problems persist, you might try the following options:

  • Restart your local machine

  • Attempt to reset Visual Studio to System Defaults (this can be done from the options within Visual Studio)

  • Attempt to repair your Visual Studio installation

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
coder771
  • 368
  • 5
  • 17
3

For me, ASP.NET 5 is not yet required, I removed update, and then reinstalled ASP.NET 4.5. It's a bit tricky:

  1. Removed from control panel "Microsoft ASP.NET 5 RC1 Update1". After that, you will no longer open ASP.NET MVC projects
  2. Then I changed the installation of "Microsoft Visual Studio Professional 2015", clicked "Modify", and selected "Microsoft Web Developer Tools"

No longer follow the notification of update in Visual Studio 2015!!!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fiorebat
  • 3,431
  • 2
  • 18
  • 18
3

In the hopes it might narrow things down/help someone, I did an investigatory approach. For me, I initially moved the folder at C:\Users\{user}\AppData\Local\Microsoft\VisualStudio to My Documents and allowed Visual Studio to re-create it by re-launching it. This removed the errors. So I moved everything back, one-by-one, and restarted Visual Studio each time until I discovered the culprits. These folders were fine to move back in:

  • 1033 (overwrote the auto-generated copy with old)
  • Designer (was in my old copy, not initially re-created when I re-launched VS, copied it back in)
  • Extensions (overwrote the auto-generated copy with old)
  • ImageLibrary (overwrote the auto-generated copy with old)
  • Notifications (overwrote the auto-generated copy with old)
  • STemplate (was in my old copy, not initially re-created when I re-launched VS, copied it back in)
  • VTC (was in my old copy, not initially re-created when I re-launched VS, copied it back in)

These files were fine to move back in/overwrite the auto-generated ones:

  • ApplicationPrivateSettings (was in my old copy, not initially re-created when I re-launched VS)
  • ApplicationPrivateSettings.lock (overwrote the auto-generated copy with old)
  • vspdmc.lock (overwrote the auto-generated copy with old)

These files were fine to move back in. Each was in my old copy, and not initially re-created when I re-launched VS:

  • .NETFramework,Version=v4.0,Set=Framework,Hash=C958D412.dat
  • .NETFramework,Version=v4.0,Set=RecentAssemblies,Hash=0.dat
  • .NETFramework,Version=v4.5,Set=Extensions,Hash=75EAE334.dat
  • .NETFramework,Version=v4.5,Set=Extensions,Hash=497525A2.dat
  • .NETFramework,Version=v4.5,Set=Framework,Hash=5AE9A175.dat
  • .NETFramework,Version=v4.5.2,Set=Extensions,Hash=24CEEB0D.dat
  • .NETFramework,Version=v4.5.2,Set=Extensions,Hash=72AE305.dat
  • .NETFramework,Version=v4.5.2,Set=Extensions,Hash=ADF899D7.dat
  • .NETFramework,Version=v4.5.2,Set=Framework,Hash=D8E943A2.dat

These caused problems - delete these files and re-launch VS to allow it to re-create them:

  • ComponentModelCache - When I overwrote this folder's contents with my old ones (4 files: MS.VS.Default.cache, .catalogs, .err, .external), this gave me all of the errors I had gotten before about not being able to load packages when loading my project, and the "object reference not set to an instance of an object" error when trying to close VS.
  • devenv.exe.config - same as ComponentModelCache
  • .NETFramework,Version=v4.0,Set=Extensions,Hash=6D09DECC.dat - causes error output from the JavaScript Language Service, complaining of missing js files
  • .NETFramework,Version=v4.0,Set=Extensions,Hash=9951BC03.dat - causes error output from the JavaScript Language Service, complaining of missing js files
  • .NETFramework,Version=v4.5.2,Set=RecentAssemblies,Hash=0.dat - causes error output from the JavaScript Language Service, complaining of missing js files

These are the errors from those last .NETFramework files (which I do not get if I do not add them back in):

01:10:11.7550: Referenced file 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\JavaScript\References\libhelp.js' not found.
01:10:11.7550: Referenced file 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\JavaScript\References\sitetypesWeb.js' not found.
01:10:11.7550: Referenced file 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\JavaScript\References\domWeb.js' not found.
01:10:11.7550: Referenced file 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\JavaScript\References\underscorefilter.js' not found.
01:10:11.7550: Referenced file 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\JavaScript\References\showPlainComments.js' not found.

I might just need to re-install/repair the JavaScript Language Service plug-in, so it might be un-related. But definitely devenv.exe.config and ComponentModelCache need to go to correct the "object reference not set to an instance of an object" error.

vapcguy
  • 7,097
  • 1
  • 56
  • 52
  • 1
    I suffered from an issue when copying and pasting in Visual Studio 2013, removing the ComponentModelCache folder worked for me. – Edward Comeau Jul 28 '16 at 11:59
3

After trying the top answer, I found that you must also restart the computer. The error may be part of a git issue as well where restarting your computer will reset.

2

In my case (not necessarily be the solution for you, but it may be helpful for someone), the solution was:

  1. Go menu ToolsExtensions and Updates

  2. Select the Online tab from the right panel

  3. Search for the words web tools, and then select Microsoft ASP.NET and Web Tools and install it.

In my case, this was missing from my computer because of a lot of repairing operations for Visual Studio.

Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
2

I was getting an exception every time I tried to create a "new" project.

My solution was:

  1. Go menu ToolsExtensions and Updates

  2. Check the "Updates" link... There was an update to Microsoft ASP.NET and Web Tools. Install it.

That fixed it for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
moe
  • 21
  • 1
2

I was getting similar error in VS2017 while trying to publish my solution to Azure.

This algorithm helped me to resolve it:

  1. Close VS2017
  2. Open C:\Users{your user name}\AppData\Local\Microsoft\VisualStudio\ folder
  3. Rename 15.0_08edcd83 folder
  4. Start VS
  5. Publish works fine!

Note that this will remove your Visual Studio's recent files / projects from it's history and also linking to MS account.

This method is slightly different from the Top answer. Here you don't have to delete other folders except renaming abovementioned one.

greenwd
  • 135
  • 1
  • 10
1

I was getting this exception every time I created a "new" project.

My solution was:

  • Go menu Tools ? Extensions and Updates
  • Not check the "Updates" link...search "asp.net". There was an update to Microsoft ASP.NET and Web Tools. Get it as AspNetWebFrameworksTools2015_KB3137909.exe and install it.

That fixed it for me.


Unfortunately, I ran into another exception:

"Exception has been thrown by the target of an invocation"

I disabled the FIPS setting Enabled DWORD value to zero.

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\FipsAlgorithmPolicy]
Enabled=dword:00000000 And All fixed.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
orign
  • 11
  • 1
1

Goto windows+R and type %temp% and hit enter. delete the temp folder and files and then try opening the same

Muhammed
  • 51
  • 3
0

I have been fighting with this during the whole weekend. But it's pretty easy to do it. What I did was to click on repair rather than uninstall. After repairing, I clicked on restart directly. After that, everything is working perfectly. You should try this, may be it can work also for you!!!

0

Restarting Visual Studio did it for me. On the next run, it displayed a "Scanning new and updated MEF components ..." and then it executed normally.

It seems to be related to Windows update somehow, although I don't have proof.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cristian Amarie
  • 160
  • 1
  • 8
0

Delete bin and obj folders in all projects of that solution.

Seems like those folders were having old files generated by some older version of visual studio, which are not compatible with new version of visual studio.

Jay Shah
  • 3,553
  • 1
  • 27
  • 26
0

The solution to the issue when i had this earlier today was that there was an additional set of tags bolted on the end of my Web.config. Once removed the functionality returned.

Icementhols
  • 653
  • 1
  • 9
  • 11
-1

Problem: My VS 2015 was throwing up error after I installed SSDT. Whenever I clicked on Tools >> Extensions and Update it was throwing "Object reference not set to an instance of an object" error. When I tried to open a solution it threw some provider not set correctly.

Solution: After many hours of research trying out all the solutions previously mentioned by others didn't solve my problem. Every time the error popup was pointing to check the ActivityLog.xml. But I was ignoring it. After hours of frustration I decided to read the error carefully and looked into ActivitLog.xml and found that it was looking for "Could not load file or assembly 'System.Collections.Immutable, Version=1.1.37.0". So I just started searching for this assembly and stumbled upon this link https://sergeytihon.com/2015/12/01/how-to-restore-viual-studio-2015-after-update-1-dependency-dance/ and followed the steps there, which fixed my issue.

Went into %LOCALAPPDATA%/Local\Microsoft\VisualStudio\14.0\devenv.exe.config and searched for "System.Collections.Immutable" and change newVersion from 1.1.36.0 to 1.1.37.0

Final config should look like this

<dependentAssembly>
 <assemblyIdentity name="System.Collections.Immutable" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
 <bindingRedirect oldVersion="1.0.27.0-1.1.65535.65535" newVersion="1.1.37.0"/>
</dependentAssembly>
Joseph Abraham
  • 347
  • 4
  • 10