"对象引用未设置为对象的实例" - 访问数组

时间:2015-05-20 04:14:42

标签: c# arrays runtime-error

我似乎无法访问我的diceRolls,我认为它可能与深层复制有关但我已经尝试了一些无法工作的东西。在测试方法中发现错误。有什么帮助吗?

    public int[] mDiceRoll = new int[GameVariables.numberOfDice];

    public DiceRoll()
    {
        Random rnd = new Random();
        for (int i = 0; i < GameVariables.numberOfDice; i++)
        {
            mDiceRoll[i] = rnd.Next(GameVariables.minDiceValue, GameVariables.maxDiceValue + 1);
        }
    }

    public int this[int key]
    {
        get
        {
            return mDiceRoll[key];
        }
        set
        {
            mDiceRoll[key] = value;
        }
    }

    [TestMethod]
    public void RandomBehavior_DiceRoll_Constructor()
    {
        int[] rollValues = new int[GameVariables.maxDiceValue + 1];
        rollValues[0] = 1;

        DiceRoll[] testRoll = new DiceRoll[100];

        for(int i = 0; i < 100; i++)
        {
            for(int j = 0; j < GameVariables.numberOfDice; j++)
            {
                if (rollValues[testRoll[i][j]] == 0) <---- errors
                    rollValues[testRoll[i][j]] = 1;                 
            }
        }

        foreach (int i in rollValues)
        {
            if (i == 0)
                throw new ApplicationException("Value not created during randomization");
        }
    }

0 个答案:

没有答案