My first C # application and the exception of the first null object

There is no total here. This is my first attempt at C #, its console application that mimics a play called Left Right Center. In the console, I get the following:

CONSOLE

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 

FROM#

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

I have no idea what the problem is or why, and my googling turned out to be more confusing than useful.

For those who are interested, I have a little experiment working now

http://pastebin.com/jxCCW2cd

+4
source share
1 answer

This line

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

declares an array that allows you to store 2 objects of the Dice class, but each value in this array is still zero.

You need to create a cube in each slot of the array before using it in the foreach loop inside the Main method.

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

if you stop the debugger on the line

  MoveQuarter (rattle.Roll ()); 

you will see that rattle Dice is null.

EDIT: looking at your code, I found problem situations In the Roll method, you recreate a random number generator, and this is useless for randomness. (see accepted answer in this question)
Finally, the Dice array could be created and initialized just like you already did for the Players array

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

This is a complete revision of the Dice class.

 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; } } 
+8
source

All Articles