Imports System.Web.CachingModule SetCache
    Dim SetingSiteCache As New Cache    Public Sub RemovedCallback(ByVal k As String, ByVal v As Object, ByVal r As CacheItemRemovedReason)    End Sub    Public Sub AddItemToCache(ByVal sender As Object, ByVal e As EventArgs)
        If (IsNothing(SetingSiteCache("Key1"))) Then
            SetingSiteCache.Add("Key1", "Value 1", Nothing, DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.High, New CacheItemRemovedCallback(AddressOf RemovedCallback))
        End If
    End Sub    Public Sub RemoveItemFromCache(ByVal sender As Object, ByVal e As EventArgs)
        If (Not IsNothing(SetingSiteCache("Key1"))) Then
            SetingSiteCache.Remove("Key1")
        End If
    End Sub
End Module
在 Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)中运行后出现如下错误。
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 9:  
行 10:     Public Sub AddItemToCache(ByVal sender As Object, ByVal e As EventArgs)
行 11:         If (IsNothing(SetingSiteCache("Key1"))) Then
行 12:             SetingSiteCache.Add("Key1", "Value 1", Nothing, DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.High, New CacheItemRemovedCallback(AddressOf RemovedCallback))
行 13:         End If
 源文件: D:\NewZF\Config\Module\SetCache.vb    行: 11 堆栈跟踪: 
[NullReferenceException: 未将对象引用设置到对象的实例。]
   System.Web.Caching.Cache.get_Item(String key) +11
   localhost.SetCache.AddItemToCache(Object sender, EventArgs e) in D:\NewZF\Config\Module\SetCache.vb:11
   localhost.Global.Application_Start(Object sender, EventArgs e) in D:\NewZF\Global.asax.vb:44 
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573

解决方案 »

  1.   

    要是用Cache("Key1")的话?在设计时就是说未定义了.
      

  2.   

    C#中Cache的用法和Session差不多。
      

  3.   

    在c#直接是 Cache.Add(key,value,dependency,ablsoluteExpiration,slidingExpiration,priority,removeCallBack).
    不用定义,用的时候先判断失效了没有。
      

  4.   

    4. 将以下代码添加到 Global.asax 文件的代码隐藏模块中的 Application_Start 事件中: Context.Cache.Insert ("abc", "Hello", null, DateTime.MaxValue, TimeSpan.Zero);
     
    5. 备注: 在 Global.asax 文件内部使用 Cache 对象时,必须通过 Context 对象(如 Context.Cache)来访问它。 -----------------------------------------------------------------------------------
    Imports System.Web.Caching
    Module Module1
        Dim onRemove As CacheItemRemovedCallback = New CacheItemRemovedCallback(AddressOf RemovedCallback)
        Dim myContext As HttpContext = New HttpContext(Nothing)    Public Sub RemovedCallback(ByVal k As String, ByVal v As Object, ByVal r As CacheItemRemovedReason)
        End Sub    Public Sub CacheRemove()
            If (Not IsNothing(myContext.Cache("Key1"))) Then
                myContext.Cache.Remove("Key1")
            End If
        End Sub    Public Sub CacheAdd()
            If (IsNothing(myContext.Cache("Key1"))) Then
                myContext.Cache.Add("Key1", "Value 1", Nothing, DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.High, onRemove)
            End If
        End Sub
    End Module
    ---------------------------------------------------------------------------------------上面的代码通过感谢朋友 香水(24976904)也谢谢大家