0

When executing this code, I get a NullReferenceException on these lines:

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

I cant figure out what is going on. I created a dict with the right items, but when I try to add it to the list, I just get a NullReferenceException....

I've been looking around MSDN and this website for about 2 hours, but no luck. Can anyone help me out? I'm just trying to store a Dictionary, into a list.

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)
            {
            }
        }
    }
}
Marc
  • 3,905
  • 4
  • 21
  • 37
avoliva
  • 3,181
  • 5
  • 23
  • 37
  • possible duplicate of [What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) – John Saunders Feb 06 '13 at 01:15

2 Answers2

7

Your Bucket constructor is creating a local variable slots but you are trying to add somedict to the (uninitialized) Bucket member slots.

Replace

List<Dictionary<Slot, string>> slots = new List<Dictionary<Slot, string>>();

with

this.slots = new List<Dictionary<Slot, string>>();

(which is the same as)

slots = new List<Dictionary<Slot, string>>();

You will have the same issue with somedict. If you do not mean it to be a class member in Bucket, do not declare it there. If you do, do not declare it as a local variable in the Bucket constructor.

DocMax
  • 12,094
  • 7
  • 44
  • 44
  • Thanks, that was it. I "know" what a NullReference means, just didn't remember the proper C# syntax. – avoliva Feb 06 '13 at 10:31
1

Of course, if you use the more compact syntax of declaring local variables with var the problem is obvious...

var slots = new List<Dictionary<Slot, string>>();
var somedict = new Dictionary<Slot, string>();
somedict.Add(new Slot(), "s");
this.slots.Add(somedict);

As DocMax pointed out, you haven't initialized this.slots and probably meant...

this.slots = new List<Dictionary<Slot, string>>();
var somedict = new Dictionary<Slot, string>();
somedict.Add(new Slot(), "s");
this.slots.Add(somedict);

I suspect the declaration of the Bucket.somedict field is probably redundant as you are creating a local somedict and then adding it to the list where it can be retrieved later.

Marc
  • 3,905
  • 4
  • 21
  • 37
StarNamer
  • 650
  • 1
  • 11
  • 27