我的第一个 c# 应用程序和第一个空对象异常

本文关键字:第一个 对象 异常 应用程序 我的 | 更新日期: 2023-09-27 18:37:27

这里的菜鸟总数。这是我第一次尝试 c#,它是一个控制台应用程序,模拟一个名为"左右中心"的饮酒游戏。在控制台中,我收到以下内容:

安慰

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
at LeftRightCenter.MainClass.Main (System.String[] args) [0x00038] in     /Users/apple/Projects/LearningC/LearningC/Main.cs:80 
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
 at LeftRightCenter.MainClass.Main (System.String[] args) [0x00038] in /Users/apple/Projects/LearningC/LearningC/Main.cs:80 

C#

    using System;
    namespace LeftRightCenter
    {
        class Player
        {
            //fields        
            private int _quarters = 4;
            public int Quarters {
                get{ return _quarters; }
                set{ _quarters += value; }
            }
            public Player (string name)
            {
            }   
        }
        class Dice
        {
            Random random = new Random();
            public int Roll ()
            {
                random = new Random ();
                int diceSide;
                diceSide = random.Next (0, 6);
                diceSide = (diceSide > 2) ? 3 : diceSide;
                return diceSide;            
            }
        }
        class MainClass
        {
            static int activePlayer = 0;
            static int theCup       = 0;
            static Player[] thePlayers = { 
                new Player ("Jessica"), 
                new Player ("Isaac"), 
                new Player ("Ed"), 
                new Player ("Bella"),
                new Player ("Elisa"),
                new Player ("Fake RedHead"),
                new Player ("Linda"),
                new Player ("MJ"),
                new Player ("Irene"),
                new Player("Devin")
            };
            static Dice[] theDice = new Dice[2];
            private static void MoveQuarter (int direction)
            {
                int numberOfPlayers = thePlayers.Length - 1;
                switch (direction) {
                case 0: 
                    thePlayers [activePlayer].Quarters = -1;
                    theCup++;
                    break;
                case 1:
                    thePlayers [activePlayer].Quarters = -1;
                    int leftPlayer = (activePlayer == 0) ? numberOfPlayers : activePlayer - 1;
                    thePlayers [leftPlayer].Quarters = +1;
                    break;
                case 2:
                    thePlayers [activePlayer].Quarters = -1;
                    int rightPlayer = (activePlayer == numberOfPlayers) ? 0 : activePlayer + 1;
                    thePlayers [rightPlayer].Quarters = +1;
                    break;                          
                }           
            }
            public static void Main (string[] args)
            {
                int cupEndPoint = thePlayers.Length * 4 - 1;
                while (theCup < cupEndPoint) {
                    foreach (Dice rattle in theDice) {
                        if (thePlayers [activePlayer].Quarters > 0) {
                            MoveQuarter (rattle.Roll ()); // this line seems to be the problem  
                        }                   
                    }
                    Console.WriteLine ("{0} Quarters In the Cup", theCup);
                }
            }
        }
    }

我不知道问题是什么或为什么,我的谷歌搜索已被证明更多的是令人困惑而不是有用。

对于那些好奇的人,我现在几乎没有实验工作

http://pastebin.com/jxCCW2cd

我的第一个 c# 应用程序和第一个空对象异常

此行

static Dice[] theDice = new Dice[2]; 

声明一个数组,该数组允许存储 Dice 类的 2 个对象,但此数组中的每个值仍为 null。

您需要在数组的每个插槽上创建一个骰子,然后再在 Main 方法内的 foreach 循环中使用它。

theDice[0] = new Dice();
theDice[1] = new Dice();

如果停止该行的调试器

 MoveQuarter (rattle.Roll ()); 

您将看到rattle骰子为空。

编辑:查看您的代码,我发现了一个有问题的情况在 Roll 方法中,您重新创建随机生成器,这对随机性没有好处。(请参阅本问题中接受的答案)
最后,Dice 数组可以像您已经为 Player 数组所做的那样创建和初始化

static Dice[] theDice = new Dice[2] {new Dice(), new Dice()};

这是您的骰子类的完整修订版

class Dice 
{
    private static Random random;
    public  Dice()
    {
           // create the static random generator only on the first instance
        if(random == null) random = new Random();
    }
    public int Roll () 
    { 
        int diceSide; 
        diceSide = random.Next (1, 7); 
        diceSide = (diceSide > 2) ? 3 : diceSide; 
        return diceSide;             
    } 
} 
相关文章: