English 中文(简体)
将一个项目添加到字典中,从而形成一种新思维模式
原标题:Adding an item to a dictionary results in a NullReferenceException

下面的法典在过去几个月里只翻了一番,但我并不确切地肯定为什么。 守则是头号地雷,但令我看上去是直截了当的。

Type pageType = page.GetType();

if (_pages.TryGetValue(pageType, out value))
    return value;

// The following line throws the exception
return _pages[pageType] = new MyPage(_section.Pages[page]);

[NullReferenceException: Object reference not set to an instance of an object.] System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) +210 System.Collections.Generic.Dictionary2.set_Item(TKey key, TValue value) +11

我认为,只有<代码>Type。 当它被用作字典钥匙时,它就是无效的,但显然。 这是不可能的

这部法典简单:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    _mypage = GetPage();
}

我还认为,这一错误可能与<代码>_section。 页: 1 <代码>。 页: 1 因此,我失踪了吗?

问题回答

我有过这个问题,但后来却发现这个问题相互关联。

通过在字典上插入一栏,可以防止错误:

这必须作为等级定义添加:

private static object _sync = new object();

下面加一栏:

lock (_sync)
{
    return _pages[pageType] = new MyPage(_section.Pages[page]);
}

这是为我解决的这个职位:。 在多读情景中添加一个词句:

我认为,这只是一个例外,因为在关键词典中没有任何内容,即pageType,进入者正在返回。

提 出

MyPage newPage = new MyPage(_section.Pages[page]);
_pages.Add(pageType, newPage);
return newPage;

或者如果存在:

MyPage newPage = new MyPage(_section.Pages[page]);
if (_pages.ContainsKey(pageType))
   _pages[pageType] = newPage;
else
   _pages.Add(pageType, newPage);

maybe the dictionary user defines somewhere else a custom IEqualityComparer that fails under some circumstances. Check where the code creates the dictionary to see if a custom comparer is passed. Maybe the comparer as well can be null, but this means this code never run...

我会把他们分开,以便澄清,然后看看正在发生什么。

var section = _section.Pages[page];
var newPage = new MyPage(section);
_pages.Add(pageType,newPage);
return newPage;

我认为,要么一页,要么一页,要么一页。 另一机会是:部分目标。

自TryGet以来 价值未见到是由_section造成的。 页: 1 由于例外来自理论家。 插入方法,很可能是由于网页上的平等/现金编码执行。 是否按惯例采用IEqualityComparer 或 Equals/GetHashCode 网页类型? 如何创建这些独裁者?





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签