Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6126957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:26:36+00:00 2026-05-23T16:26:36+00:00

The below code throws an error of Object reference not set to an instance

  • 0

The below code throws an error of "Object reference not set to an instance of an object" When calling mycache.Get(“products”). Im using a WCF application. Im not 100% im using caching correctly. Any advice?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Web.Caching; 

namespace DataLayer {
    public class Products
    {
        public List<Product> Current()
        {
            Cache mycache = new Cache();
            if (mycache.Get("products")== null)
            {
                using (DataLayer.AccessDataContext db = new AccessDataContext())
                {
                    var products = from p in db.fldt_product_to_supp_parts
                                   where p.Current
                                   select new Product
                               {
                                   WholesaleCode = p.WholesaleCode,
                                   ProductCode = p.Product_Code
                               };
                    mycache["products"] = products.ToList();
                }

            }
            return mycache["products"] as List<Product>;
        }

    } }

EDIT : I’m using .net 3.5

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:26:37+00:00Added an answer on May 23, 2026 at 4:26 pm

    From the look of the documentation you shouldn’t be creating your own instance of the Cache class (it says the constructor is for framework use only). Try using Cache.Get instead?

    EDIT (in response to comment)…

    From the MSDN docs here:

    One instance of this class is created per application domain, and it remains valid as long as the application domain remains active. Information about an instance of this class is available through the Cache property of the HttpContext object or the Cache property of the Page object.

    So, it looks like Cache.Get is available when you’re within a Page; otherwise you can call HttpContext.Cache to get the active cache. Either way, there’s a single Cache object for your entire application and you definitely shouldn’t be creating one of your own.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The below piece of code that throws the following exception.. Error Message: Object reference
e.g. The code below throws a ClassCastException when the second Object is added to
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
I have handling the exception in my code as below, but still get error
The below code in Java throws Null pointer exception. public class New{ int i;
Hi I have below pseudo code with throws an exception like this throw new
Below code saying error incorreect syntax near Main INSERT INTO tbl ( 'Week', Main,
INITIAL QUESTION (UPDATED BELOW) I'm using an AutoPopulatingList list to instatiate an object invoking
I have some code that performs a deep copy using Object.clone, but I'm trying
I get an error on the one of my lines of code in Internet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.