I encountered this error: NullReferenceException: Object reference not set to an instance of an object
When creating the countStats object, it is NULL, after which I try to assign a value to the field of this class and get this error. What am I doing wrong?
public async Task<MainPageViewModel> GetMainPageViewModelAsync()
{
var model = new MainPageViewModel();
var countStats = new StatsCountsViewModel(); // NULL
if (!_cache.TryGetValue("CountsStats", out countStats))
{
var result = await _contextRepository.GetStatsMainPageAsync();
countStats.CountSms = "300"; //ERROR
countStats.CountUsers = "600";
countStats.CountSuccessForecast = "1228";
model.Stats = countStats;
_cache.Set("CountsStats", countStats, new MemoryCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(10)
});
}
var forecasts = await _contextRepository.GetForecastsOnSaleAsync();
foreach (var item in forecasts)
{
model.Forecasts.Add(item);
model.Bookmaker.Add(await _contextRepository.GetBookmakersForForecastsAsync(item.Id));
}
return model;
}
public class StatsCountsViewModel
{
public string CountUsers { get; set; }
public string CountSms { get; set; }
public string CountSuccessForecast { get; set; }
}