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

11,316

Solution 1

It is a common problem with the use of the new protocol (version 3) nuget used in Visual Studio 2015. It is with multiple nuget packages, for example Microsoft.Owin 3.0.1. To fix this go to:

C:\Users\<your user name>\AppData\Roaming\NuGet

And edit the XML configuration file, commenting the line of protocol 3:

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

Maintenance settings for version 2. Save and restart visual studio 2015. You should now restore the nuget packages without problems.

Solution 2

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
Share:
11,316
m.ghadiri
Author by

m.ghadiri

Updated on July 14, 2022

Comments

  • m.ghadiri
    m.ghadiri almost 2 years

    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 ==========
    
  • m.ghadiri
    m.ghadiri over 8 years
    The problem is I never installed them, there is no packages.config and there is no another assembly of the package!! I have 7 project in my solution and diffrent packages in each of them.
  • Rico Suter
    Rico Suter over 8 years
    The avoid the problem 4), you can use Project Dependency Browser (projectdependencybrowser.org) which can analyze a C# project for NuGet/DLL referencing problems...