0

First off, I'm using the NUnit.framework in c# to run tests. I'm building a card game (the game Dominion) in c#. I'm trying to writer a unit test that tests that when I shuffle my discard pile into my deck, the new shuffled deck contains all the same cards.

I'm getting an error in a helper method I wrote to compare if 2 dictionaries representing the count of the cards are equivalent. (Dictionary<Card, int>, where Card is a custom class that is hashed by an id field each instance has, and int being how many of that card a deck contains.

Right now, I'm getting a NullReferenceException on the line between the prints "before" and "after". Here's the helper method:

this code is called from within an Assert.True statement, which is the line that NUnit reports as having the error, but the print statement "before" is the last thing to print to the console in the first loop (the loop should run twice).

I'm new to c# and I don't really understand how this works, so I could use some help.

[edit] added unit test code.

[Test()]
public void testShuffledDeckContainsSameCards()
{
    ArrayList list = new ArrayList();
    Stack<Card> shuffledDeck;
    Dictionary<Card, int> cardCount;
    Dictionary<Card, int> expect = new Dictionary<Card,int>();
    for (int i = 0; i < 7; i++)
    {
        list.Add(new KingdomCards.Copper());
    }
    expect.Add(new KingdomCards.Copper(), 7);

    shuffledDeck = (Stack<Card>) Player.Shuffle(list);
    cardCount = countCards(shuffledDeck);
    //CompareCounts(expect, cardCount);

    list.Add(new KingdomCards.Estate());
    list.Add(new KingdomCards.Estate());
    list.Add(new KingdomCards.Estate());
    expect.Add(new KingdomCards.Estate(), 3);

    shuffledDeck = (Stack<Card>) Player.Shuffle(list);
    cardCount = countCards(shuffledDeck);
    Assert.True(CompareCounts(expect, cardCount));
}

private static bool CompareCounts(Dictionary<Card, int> expect, Dictionary<Card, int> cardCount)
{
    foreach (KeyValuePair<Card, int> entry in 
    {
        Console.WriteLine("Entry:");
        Console.WriteLine("card " + entry.Key.getID() + ", count " + entry.Value);
        Console.WriteLine("expected:");
        if (!cardCount.ContainsKey(entry.Key))
        {
            return false;
        }
        Console.WriteLine("before");
        Console.WriteLine("card " + entry.Key.getID() + ", count " + cardCount[entry.Key]);
        Console.WriteLine("after");
        Console.WriteLine();
        if (!(entry.Value == cardCount[entry.Key]))
        {
            return false;
        }
    }
    return true;
}
icaughtfireonce
  • 171
  • 1
  • 10
  • 3
    can you post the full test method? – Ewan Apr 20 '15 at 15:29
  • ok i think i see it. theres no guarentee that cardCount contains key entry.Key ie your cardCount dictionary dosen't contain a card which is in your expect – Ewan Apr 20 '15 at 15:33
  • the crucial part is missing: `foreach (KeyValuePair entry in ` in what? – thumbmunkeys Apr 20 '15 at 15:34
  • It's tempting to mark this as a duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it), as that's what it essentially boils down to here. The debugging tools at your disposal should help you fix this very quickly. – James Thorpe Apr 20 '15 at 15:39
  • Look here for information and check each object on that line in the debugger to see which one is actually null and then figure out why that is http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Steve Mitcham Apr 20 '15 at 15:45
  • I've been unable to get the debugger to run on this code. Every time I try to run the debugger, it runs on the GUI code that someone else in the project wrote. (I'm using visual studio 2013). – icaughtfireonce Apr 20 '15 at 15:57

0 Answers0