执行此代码时,我在这些行上收到 NullReferenceException:
List<Dictionary<Slot, string>> slots = new List<Dictionary<Slot, string>>();
Dictionary<Slot, string> somedict = new Dictionary<Slot, string>();
somedict.Add(new Slot(), "s");
this.slots.Add(somedict);
我无法弄清楚发生了什么。我用正确的项目创建了一个字典,但是当我尝试将它添加到列表中时,我只得到一个 NullReferenceException ....
我已经在 MSDN 和这个网站上看了大约 2 个小时,但没有运气。谁能帮我吗?我只是想将字典存储到列表中。
namespace hashtable
{
class Slot
{
string key;
string value;
public Slot()
{
this.key = null;
this.value = null;
}
}
class Bucket
{
public int count;
public int overflow;
public List<Dictionary<Slot, string>> slots;
Dictionary<Slot, string> somedict;
public Bucket()
{
this.count = 0;
this.overflow = -1;
List<Dictionary<Slot, string>> slots = new List<Dictionary<Slot, string>>();
Dictionary<Slot, string> somedict = new Dictionary<Slot, string>();
somedict.Add(new Slot(), "s");
this.slots.Add(somedict);
for (int i = 0; i < 3; ++i)
{
}
}
}
}