'Nuget - Object reference not set to an instance of an object

I have a problem with nuget package manager in vs 2015. Some packages like Unity has been installed with no problem. Some packages like EF had problems while installing. Some packages like Automapper had the same problem but when I installed another version of this package, It installed fine.

here is the output:

System.NullReferenceException: Object reference not set to an instance of an object.


at NuGet.Protocol.Core.v3.GlobalPackagesFolderUtility.<AddPackageAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at NuGet.Protocol.Core.v3.DownloadResourceV3.<GetDownloadResourceResultAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.PackageManagement.PackageDownloader.<GetDownloadResourceResultAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__42.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__42.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.PackageManagement.UI.UIActionEngine.<ExecuteActionsAsync>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.PackageManagement.UI.UIActionEngine.<PerformActionAsync>d__3.MoveNext()
========== Finished ==========


Solution 1:[1]

Same problem still exists in Visual Studio 2022. The solution:

  1. Navigate to C:\Users<your user name>\AppData\Roaming\NuGet // remember AppData is a hidden folder, so you will have to make Windows show hidden files and folders before you can see it
  2. Open NuGet.Config in Notepad (or whatever will let you edit it)
  3. In the "packageSources" section, look for the line:

add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3"

and delete 'protocolVersion="3"'

  1. Save the file and restart Visual Studio 2022

Solution 2:[2]

When i've had Nuget problems in the past these are the steps i've done:

1) Use Nuget package manager to remove out of project(s)

2) Clean and Build (you'll have errors because you do longer have the references 2) Make sure the packages were removed from your packages.config (if not, delete and repeat step 2)

3) Make sure the .dlls were removed from your bin folder (if not, delete and repeat step 2)

4) Add packages back in and rebuild. Ensure the packages were added to packages.config (with appropriate version) and .dlls were added back to the bin folder (with appropriate version).

Solution 3:[3]

I am running VS 2017, but was having this error with the TFS build server during the "dotnet restore" task. The issue was we had an internal local file share, but the nuget.config had protocolVersion="3" for that entry. After removing that property, the build was successful.

Solution 4:[4]

  1. My problem happens in NuGet.Config (he NuGet.Config file path: C:\Users\<UserName>\AppData\Roaming\NuGet)

  2. Reason?Tools -> NuGet Package Manager -> Package Management Settings -> Package Source ->

    nuget.org=https://api.nuget.org/v3/index.json
    

    The default package source has attribute protocolVersion="3"?but I modified it to my offline package source, meanwhile creating a new package source?

    nuget.ort=https://api.nuget.org/v3/index.json
    

    The new package source has no attribute protocolVersion="3",so I always fail to install packages whether of online or offline.

  3. Solution: Add ProtocalVersion="3" to

    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3"/>
    

    and remove ProtocalVersion="3" in my offline package source.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Will Buffington
Solution 2 Roka
Solution 3 Robert Taylor
Solution 4 Jeremy Caney