C# 字典TryGetValue NullReferenceException
我在C# 字典TryGetValue NullReferenceException,c#,.net,dictionary,nullreferenceexception,C#,.net,Dictionary,Nullreferenceexception,我在\u dicCache.TryGetValue(objID,out newObject)处获得NullReferenceException 线路。我完全不知道为什么会这样。请给我指一下正确的方向好吗 这是我的班级: public class Cache<T> { public string Name { get; set; } private Dictionary<int, T> _dicCache = new Dictionary<int,
\u dicCache.TryGetValue(objID,out newObject)处获得NullReferenceException代码>
线路。我完全不知道为什么会这样。请给我指一下正确的方向好吗
这是我的班级:
public class Cache<T>
{
public string Name { get; set; }
private Dictionary<int, T> _dicCache = new Dictionary<int, T>();
public void Insert(int objID, T obj)
{
try
{
_dicCache.Add(objID, obj);
HttpContext.Current.Cache.Insert(Name, _dicCache, null, DateTime.Now.AddMinutes(10), TimeSpan.FromMinutes(0));
}
catch (Exception)
{
throw;
}
}
public bool Get(int objID, out T obj)
{
_dicCache = (Dictionary<int, T>)HttpContext.Current.Cache.Get(Name);
try
{
return _dicCache.TryGetValue(objID, out obj);
}
catch (Exception)
{
throw;
}
}
}
公共类缓存
{
公共字符串名称{get;set;}
私有字典_dicCache=新字典();
公共无效插入(内部对象,T对象)
{
尝试
{
_添加(objID,obj);
HttpContext.Current.Cache.Insert(名称,_dicCache,null,DateTime.Now.AddMinutes(10),TimeSpan.frommines(0));
}
捕获(例外)
{
投掷;
}
}
公共目标获取(内部目标,外部目标)
{
_dicCache=(Dictionary)HttpContext.Current.Cache.Get(Name);
尝试
{
返回_dicCache.TryGetValue(objID,out obj);
}
捕获(例外)
{
投掷;
}
}
}
我这样称呼它:
Services.Cache<Entities.User> cache = new Services.Cache<Entities.User>();
cache.Name = Enum.Cache.Names.usercache.ToString();
Entities.User user = new Entities.User();
cache.Get(pUserId, out user);
Services.Cache Cache=new Services.Cache();
cache.Name=Enum.cache.Names.usercache.ToString();
Entities.User=新的Entities.User();
Get(pUserId,out用户);
我还尝试改变课程设置,以便:
public T Get(int objID, out T obj)
{
_dicCache = (Dictionary<int, T>)HttpContext.Current.Cache.Get(Name);
T newObject = (T)Activator.CreateInstance<T>();
try
{
_dicCache.TryGetValue(objID, out newObject);
obj = newObject;
return obj;
}
catch (Exception)
{
throw;
}
}
public T Get(内部对象,外部对象)
{
_dicCache=(Dictionary)HttpContext.Current.Cache.Get(Name);
T newObject=(T)Activator.CreateInstance();
尝试
{
_TryGetValue(objID,out newObject);
obj=新对象;
返回obj;
}
捕获(例外)
{
投掷;
}
}
但是我仍然在\u dicCache.TryGetValue(objID,out newObject)中得到相同的NullReferenceException代码>行。实际将dicCache放入http上下文缓存的方法是insert方法,该方法从未在代码中调用过,因此当您尝试从http上下文获取它时,会得到null(您只调用过get)
我将更改名称设置器,以便在当时将字典实际放入http上下文中,或者更好,如果您可以通过将Name属性作为构造函数参数以某种方式将字典插入构造函数中的缓存中。一般来说,我试图以这样一种方式来设计类,使它们在尽可能短的时间内处于“未初始化”状态。我认为唯一可能出现这种异常的方法是如果字典为空
_dicCache.TryGetValue(objID, out newObject);
null
是键的有效参数(如果TKey
是引用类型),但在您的示例中是int
您确定\u dicCache
不是null
?我会检查分配的值:
_dicCache = (Dictionary<int, T>)HttpContext.Current.Cache.Get(Name);
\u dicCache=(Dictionary)HttpContext.Current.Cache.Get(Name);
\u dicCache
可能为空。您确定(字典)HttpContext.Current.Cache.Get(名称)代码>此强制转换是否有效?调试这些行您不需要初始化newObject
,因为它是由TryGetValue
初始化的。问题绝对不是它。@Konstantin Vasilcov\u dicCache
是null
!我没有注意到这一点。非常感谢。因为我像这样声明\u dicCache
变量:私有字典\u dicCache=new Dictionary()我以为它不会是空的。你能解释一下为什么它是空的吗?缓存中的Name
找到了什么吗?是的,我的_dicCache是空的:(.试图解决这个问题…因为我声明了_dicCache变量,如下所示:private Dictionary _dicCache=new Dictionary()我以为不会的null@rpmlins,我猜是从HttpContext中分配的。Current.Cache
正在将其设置为null
。嗯,似乎有人不喜欢我。如果你看我下面的答案,它正好解释了这一点。在你的Insert方法中,你把_dicCache放在http上下文中。在你的Get方法中,你覆盖了member_dicCache变量来自http上下文缓存中的内容,该变量始终为null,因为您从未调用Insert方法。_dicCache=(字典)HttpContext.Current.cache.Get(名称)
将始终返回null,除非调用Insert
方法,该方法将非null的dicCache放入http上下文的缓存中。@AmeenTayyebiJazayeri,请向上投票。嘿@Ameen我同意你的答案,但我没有像我在问题上所说的那样调用Insert方法。我只是调用Get方法。这是故意的.我只在对象不在缓存中时插入。因此我总是先调用Get。