Object reference not set to an instance of an object. MemoryCache.Default.Get(guidToken) as ResponseObject;

I'm getting the following error on my objResponse set to null:

Object reference not set to an instance of an object.... This

var searchCriteria = objResponse.Model as InvoicePrintingParameters;  My objresponse is set to null



       private void AddEmailedCustomersToCacheObject(string guidToken, List<int> emailedCustomers)
        {
            var objResponse = MemoryCache.Default.Get(guidToken) as ResponseObject;
            var searchCriteria = objResponse.Model as InvoicePrintingParameters;

            searchCriteria.listEmailedCustomers = emailedCustomers;

            ResponseObject result = new ResponseObject() { Success = objResponse.Success, Message = objResponse.Message, Model = searchCriteria };
            MemoryCache.Default.Remove(guidToken);
            MemoryCache.Default.Add(guidToken, result, DateTime.Now.AddMinutes(1));
        }

Open in new window

yguyon28Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
As per the design of the ObjectCache, if the item with a given key is not found it will return null. Are you sure that item exists in the cache?
0
yguyon28Author Commented:
I think what I'm trying to do is if the memory cache is null is to write a better logic than what I have created.
0
Chinmay PatelChief Technical NinjaCommented:
Something like this? because if
            var objResponse = MemoryCache.Default.Get(guidToken) as ResponseObject;

Open in new window

for some reason then can not proceed further.

       private void AddEmailedCustomersToCacheObject(string guidToken, List<int> emailedCustomers)
        {
            var objResponse = MemoryCache.Default.Get(guidToken) as ResponseObject;
if(objResponse == null )
{
// handle the issue
// you might return; from here or do something else, maybe log the exception - if that is the case.
}else
{
            var searchCriteria = objResponse.Model as InvoicePrintingParameters;

            searchCriteria.listEmailedCustomers = emailedCustomers;

            ResponseObject result = new ResponseObject() { Success = objResponse.Success, Message = objResponse.Message, Model = searchCriteria };
            MemoryCache.Default.Remove(guidToken);
            MemoryCache.Default.Add(guidToken, result, DateTime.Now.AddMinutes(1));
}
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.