【问题标题】:Dictionary TryGetValue NullReferenceException [closed]字典 TryGetValue NullReferenceException [关闭]
【发布时间】:2012-12-26 00:09:27
【问题描述】:

我在_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;
        }
    }
 }

我是这样称呼它的:

   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);

我也尝试将 Get 类更改为:

    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;
        }
    }

但我仍然在 _dicCache.TryGetValue(objID, out newObject); 行得到相同的 NullReferenceException。

【问题讨论】:

  • _dicCache 可能为空。你确定(Dictionary&lt;int, T&gt;)HttpContext.Current.Cache.Get(Name); 这个演员表有效吗?调试这些行
  • 你不需要初始化newObject,因为它是由TryGetValue初始化的。问题绝对不在于它。
  • @Konstantin Vasilcov _dicCachenull!我没有注意那个。非常感谢。
  • 因为我像这样声明_dicCache 变量:private Dictionary&lt;int, T&gt; _dicCache = new Dictionary&lt;int, T&gt;(); 我认为它不会为空。你能解释为什么它是空的吗?
  • 那个Name在缓存中有什么发现吗?

标签: c# .net dictionary nullreferenceexception


【解决方案1】:

我认为,如果您的字典为空,您可能会遇到该异常的唯一方法。

_dicCache.TryGetValue(objID, out newObject);

null 是键的有效参数(如果 TKey 是引用类型),但在您的情况下它是 int

您确定_dicCache 不是null?我会检查分配的值:

_dicCache = (Dictionary<int, T>)HttpContext.Current.Cache.Get(Name);

【讨论】:

  • 是的,我的 _dicCache 为空 :(。试图解决这个问题......因为我声明了 _dicCache 变量是这样的:private Dictionary _dicCache = new Dictionary() ; 我认为它不会为空
  • @rpmlins,我猜来自HttpContext.Current.Cache 的分配将其设置为null
  • 好吧,好像有人不喜欢我。如果您在下面查看我的答案,则可以完全解释这一点。在您的 Insert 方法中,您将 _dicCache 放在 http 上下文中。在您的 Get 方法中,您从 http 上下文缓存中的内容覆盖成员 _dicCache 变量,该变量始终为 null,因为您从不调用 Insert 方法。
  • _dicCache = (Dictionary&lt;int, T&gt;)HttpContext.Current.Cache.Get(Name); 将始终返回 null,除非调用 Insert 方法将非 null _dicCache 放入 http 上下文的缓存中。
  • @AmeenTayyebiJazayeri,请给我点赞。
【解决方案2】:

实际上将 _dicCache 放入 http 上下文缓存的方法是 insert 方法,该方法从未在您的代码中调用,因此当您尝试从 http 上下文中获取它时,您会得到 null(您只调用 Get)。

我会更改 Name 设置器以在那时将字典实际放入 http 上下文中,或者更好的是,如果您可以通过将 Name 属性作为构造函数参数以某种方式将字典插入构造函数的缓存中。一般来说,我尝试以这样一种方式设计类,使它们在尽可能短的时间内处于“未初始化”状态。

【讨论】:

  • 嘿@Ameen 我喜欢你的回答,但我没有像我在问题中所说的那样调用插入方法。我只是在调用 Get 方法。那是故意的。我只在对象不在 _dicCache 中时插入。所以我总是先调用 Get。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-16
  • 2023-03-05
  • 2012-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多