Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to get Cache gives "object reference not set to instance" error

Status
Not open for further replies.

TeaAddictedGeek

Programmer
Apr 23, 1999
271
US
I have no idea why this isn't working, and right now I have this statement around a try/catch block to look at the error:

object Cached = HttpContext.Current.Cache[key];

Any idea on why attempting to get a key value from the cache would produce this error? I'm trying to test if the value exists and if not, place it in the cache.


Thanks!

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
I'm not sure if this makes a difference but shouldn't you be using the Get method e.g.
Code:
object Cached = HttpContext.Current.Cache[b].Get[/b][key];


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Yeah, I tried both ways and I get the same error.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
Where are you calling this method from? Can you post an example so we can try to replicate the problem?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
stupid question in some cases... but have you ensured that the value of "key" is set.

Also, does is value of "key" exist in the Cache()?

Senior Software Developer
 
this should work if its not there

Code:
if (!(HttpContext.Current.Cache[key]==null))
{
    //it exists
}
else
{
    //it doesnt exist
}
 
adamroof, I tried that too. Gives the same error.

I'm trying to test for the existence of this key in the cache and it seems no matter what I try, it produces that object reference error.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
Is this .Net 2? If not, you can probably ignore the below...

...anyway, I had some fairly serious issues with Caching in .Net 2 that appear to be related to some of the underlying changes to Caching and the CachItemPriority. Basically when converting an old project utilising Caching, the older methods writing to the cache didn't set a CacheItemPriority as per .Net 2 and it appears as if, (at least on my work Dev machine with way too little memory at 512mb), this was causing items to be removed from the Cache to free 'server', (i.e., VS Development Web Server), resources as per the .Net documentation.

I simply changed the older methods making calls to Cache.Insert to strictly control the Sliding and Absolute expiration, the CachePriority, and to allow a delegate CacheItemRemovedCallback method to be passed in, (or not), as required so I could ascertain when my erroneously dissapearing items were being removed.

Hope this helps, the explanation of the issue you're experiencing is awfully familiar :)

Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
This is a brand new application starting from scratch. I'm making a call to determine if an object is in cache and if so, grab it. If not, make call to DB to get the appropriate info then store it in cache.

I'm using VS 2005, and while I love what they've done with the debugger I am NOT liking what they've done with other aspects of the IDE.

Everywhere I've looked on retrieving cache would say that I can test to see if a key value is null, and that would mean that the object doesn't exist in cache. I haven't seen anything on the error that I'm getting and it's very weird.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
Where are you calling this method from? Can you post an example so we can try to replicate the problem?
You haven't responded to this yet.

I am NOT liking what they've done with other aspects of the IDE
What don't you like?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I'm not sure what more to post which is why I didn't respond. I'm calling a public method in a public class to attempt to get the cache key--what more detail could you possibly need?

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
I'm calling a public method in a public class to attempt to get the cache key--what more detail could you possibly need?
So show us this class and method. I can't see your project and I'm not going to make a guess at what could be wrong without seeing your code.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
It's pretty simple....

public class CacheTesting
{
public void GetRawCacheTest()
{
try
{
if (!(HttpContext.Current.Cache ["PioneerParameterList"] == null))
{
}

}
catch (Exception e)
{
Console.WriteLine("Error in GetRawCacheTest() get value as string:");
Console.WriteLine(e.Message);
Console.Write(e.StackTrace);
}
}
}

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
OK, and now an example of this bit:
ca8msm said:
Where are you calling this method from?



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Are you sure the null reference exception is against the object in the cache and not the context or cache itself? Oh, and why, (shher morbid curiosity) ...
Code:
 if (!(HttpContext.Current.Cache ["PioneerParameterList"] == null))

rather than
Code:
 if (HttpContext.Current.Cache ["PioneerParameterList"] != null)


Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
Rhys: Yeah I've done both, neither worked.

ca8msn: Right now I'm trying to NUnit test this, so that's what's running the class. I also tried creating a separate application that calls the function, but no dice.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
i got this to work,
Code:
    //Test Area
    public void getCache(object sender, EventArgs e)
    {
        lblErrMsg.Text = testCache();
    }

    public void clearCache(object sender, EventArgs e)
    {
        HttpContext.Current.Cache.Remove("testingCache");
    }

    public string testCache()
    {
        if (!(HttpContext.Current.Cache["testingCache"] == null))
        {
            return HttpContext.Current.Cache["testingCache"].ToString();
        }
        else
        {
            HttpContext.Current.Cache["testingCache"] = "Here Is Some Information";
            return "Just Added Some Information, Run It Again To See The Results";
        }
    }

and re: Rhys66
i tried both !( == null) and != null with same results.
ive just come to like the !(==null) because i had better results when testing for query strings, the !=null seemed to not test true sometimes.

 
The actual class that you've written works fine in the test that I did so it is something else that is casuing the problem. What this "something else is" is something you will have to figure out on your own (as you still haven't provided any code on how you set the value and how the method is called in your sample application) so there is no way we can replicate your problem or guess what you are doing.

In my test, I set the cache value like this, and then called the class:
Code:
        HttpContext.Current.Cache.Insert("myTest", "hello")
        Dim x As New CacheTesting
        x.GetRawCacheTest()


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
try

public class classname : Page //inherits the web form


and/or be sure that you are using

using System;
using System.Web;
 
ca8msm: So you have to insert a cache value before you test for its existence? There's no way that you could simply just test for it to see if it is there, and if not, THEN set it? Because as I said before, that's what I'm attempting to do hence why I have no supplied any code where I set the value, for it doesn't get set until I'm certain that the value doesn't exist to begin with!



"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top