0

Student here.

Working on a current C# project.

I have to create a List<Card> of playing cards, and a Dictionary<string, List<Card>>.

List gets created independently, and the dictionary holds collections of cards. Cards can be added or removed from collections.

I'm having trouble creating a dictionary key based off user input

The error is while the program is running, and I type in the name of the key I want to create.

Object reference not set to an instance of an object.

Here is the code block:

Console.Write("What is the name of this collection? ");
string collectionName = Console.ReadLine();
bool found = currentManager.Collections.ContainsKey(collectionName);
if (!found)
 {
    currentManager.Collections.Add(collectionName, null);
 }
else
    Console.WriteLine("That collection name has already been used.");

I am using a CollectionManager class, with my Dictionary named _collections. This is being called here:

public Dictionary<string, List<Card>> Collections
    {
        get
        {
            return _collections;
        }
        set
        {
            _collections = value;
        }
    }
George Alexandria
  • 2,643
  • 2
  • 14
  • 24
  • Something is not defined, either CollectionManager doesn't have dictionary defined or an instance is not made of one of the classes when needed. Hard to tell given the code you provided. – Adrian Sep 10 '17 at 22:41
  • Is there any other portion of the code I could provide that may help determine what isn't defined? Just didn't want to post everything and have an unreadable amount of code compared to the broken section. – Dylan Cole Duke Sep 10 '17 at 22:47
  • I think that the problem is inside your setter for Collections. Your private dictionary `_collections` might be not initiallized with new instance of Dictionary. So in the place where you have `private Dictionary> _collections`. It should be `private Dictionary> _collections = new Dictionary>(); ` Please check this out. I had the same pain when I just started to use properties in c#. – Mitrucho Sep 10 '17 at 23:26
  • @DylanColeDuke Usually, a whole class for CollectionManager for example would help determine if 'Collections' is defined. If that's all the code you have, then Collections is not defined. – Adrian Sep 11 '17 at 07:48
  • You're adding `null` as the value of the key... – pinkfloydx33 Sep 11 '17 at 10:36
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – pinkfloydx33 Sep 11 '17 at 10:37
  • @pinkfloydx33 can the value not be null? I want the user to be able to inter the value in separately, so what can I put instead as a placeholder until the user enters the value? – Dylan Cole Duke Sep 11 '17 at 19:33

2 Answers2

0

Is the _collections reference initialized to a Dictionary instance? Based on the error message, it looks like _collections is not pointing to an instance.

Peter_McL
  • 1
  • 1
0

Change your property to:

public Dictionary<string, List<Card>> Collections {get;set;} = new Dictionary<string, List<Card>>(); 
KnowHoper
  • 3,967
  • 2
  • 31
  • 45