2011-08-16 31 views
0

我收到以下錯誤。參數null異常未被用戶代碼處理

參數null異常未被用戶代碼處理。 值不能爲空。 參數名稱:值

在粗體位置的下面的代碼。

public static void InsertIntoCache(string cacheKey, object value) 
{ 
    HttpRuntime.Cache.Insert(cacheKey, value, null, DateTime.Now.Add(new TimeSpan(0, CacheDuration, 0)), **Cache.NoSlidingExpiration**, CacheItemPriority.Default,null); 
} 

請幫助我。

+0

很肯定不能在你指出的位置。 'Cache'由ASP .NET運行時設置,屬性是'DateTime'的枚舉值。你的變量'value'等於'null'嗎? – Yuck

回答

2

Cache.Insert()的方法簽名(從MSDN拍攝):

public void Insert (
string key, 
Object value, 
CacheDependency dependencies, 
DateTime absoluteExpiration, 
TimeSpan slidingExpiration, 
CacheItemPriority priority, 
CacheItemRemovedCallback onRemoveCallback 
) 

錯誤是告訴你,value,這是在.Insert()方法的第二個參數,不能爲空。這意味着,你的方法被調用,使用null作爲第二個參數,如:

InsertIntoCache("some_key", null); // this will throw an exception. 

基本上,你不能緩存「空」的值。如果您在this link到「例外」向下滾動,它指出插入()將拋出一個ArgumentNullException時:

鍵或值參數爲空引用

+0

謝謝拉力賽... – shaker

相關問題