[Solved] New class object error : Object reference not set to an instance of an object [duplicate]


This is your problem:

_cache.TryGetValue("CountsStats", out countStats)

out will change the object that countStats is pointing to. If it’s found in the cache, then it will be the cached object. Great! If it isn’t, then it will be null. You need to create a new instance in this case.

You should change your code to this:

var countStats = null;

if (!_cache.TryGetValue("CountsStats", out countStats))
{
    countStats = new StatsCountsViewModel();

solved New class object error : Object reference not set to an instance of an object [duplicate]