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 Shaun E 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
 
So you have to insert a cache value before you test for its existence?
No, you don't have to set it but by doing this it will at least test to see if the value can be instered and retrieved from the cache.

If you produce this test and paste the entire test (i.e. the code you use to set the key, the code you use to insert the value, the code you use to call the class and function and the code from the class) it will allow us to attempt to replicate exactly what you are doing. Without this, we'll just end up another 20 reponses down the line and no further forward, just like we are now.


____________________________________________________________

Need help finding an answer?

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

 
Of course, had your suggestion been suggested 20 posts back I could easily say the same thing.

I tried it no luck--I can't insert either. I get the same error.

[TestClass]
public class CacheTesting
{
[TestMethod]
public void InsertIntoCacheTest()
{
try
{
HttpContext.Current.Cache.Insert("TestCache", "hello");
}
catch (Exception e)
{
Console.WriteLine("Error in InsertIntoCacheTest():");
Console.WriteLine(e.Message);
Console.Write(e.StackTrace);
}
}

[TestMethod]
public void GetRawCacheTest()
{

try
{
if (!(HttpContext.Current.Cache["TestCache"] == null))
{
}

}
catch (Exception e)
{
Console.WriteLine("Error in GetRawCacheTest():");
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
 
Of course, had your suggestion been suggested 20 posts back I could easily say the same thing.
I asked in my second post for an example that would allow us to replicate a problem. This still has not been given as you haven't provided all the code you are using for this test. What you've provided above doesn't still doesn't contain all of this:
ca8msm said:
If you produce this test and paste the entire test (i.e. the code you use to set the key, the code you use to insert the value, the code you use to call the class and function and the code from the class)
As you obviously can't understand what is needed of you to get help with this problem, and I've repeated this at least 4 times, I can't help any further with this thread.


____________________________________________________________

Need help finding an answer?

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

 
I tried it no luck--I can't insert either. I get the same error

This is strongly suggestive of a NULL Context or Cache. Out of sheer morbid curiosity, are you sure you've tried something like;
Code:
public class CacheTesting
{
	public void InsertIntoCacheTest()
	{
		try
		{
			if( HttpContext.Current == null )
			{
				throw new Exception("The Current HttpContext is NULL");
			}
			else if(HttpContext.Current.Cache == null)
			{
				throw new Exception("The Current HttpContext Cache is NULL");
			}
			else
			{
				HttpContext.Current.Cache.Insert("TestCache", "hello");
			}
		}
		catch( Exception e )
		{
			Console.WriteLine( "Error in InsertIntoCacheTest():" );
			Console.WriteLine( e.Message );
			Console.Write( e.StackTrace );
		}
	}
}
...to verify the Current Context and Cache?

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
 
Ah! Thank you, Rhys. I am new to caching and have Googled from here to kingdom come and can't figure out why this isn't working. I appreciate your patience and willingness to help.

I tried your code and it would appear that the HttpContext is indeed null. Is this something that has to be initialized somehow before using it?


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 dont see a NullReferenceException here,

usually msdn will state that. hmmmmm... it needs the System.Web

maybe i missed it, but, where is the class located and consumed?

if a 2.0 app_code class, then try adding the using statements at the top (like my prev post, didnt hear if you tried that or not)

Code:
using System;
using System.Web;

public class CacheTesting : Page
{
    public void InsertIntoCacheTest()
    {
        try
        {
            if( HttpContext.Current == null )
...

Does intellisense kick in when you type HttpContext, or hover over does the tooltip display?
 
I imagine the problem is because you're calling the class from NUnit and not from the context of a Web Application/Web Service etc. Thus, NUnit and your test is actually the problem here as the same code called from a simple 'test' web page would function as you were expecting it too.

Not knowing much about NUnit I probably can't help you much further, but I imagine you need to spoof a Web Context when calling the method in the test bed.

Glad I could help get you to the bottom of the exception tho'.

:)

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
 
Yeah, that's what I was starting to wonder--whether or not NUnit itself was the culprit.

I have System.Web and System.Web.Caching in there, or it wouldn't recognize any of the caching commands in there.

I'm going to try to create a separate web app to see if these commands will work there. So much for unit testing using standard apps. :p If I continue to have problems, I'll post it here. 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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top