Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8130475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:43:26+00:00 2026-06-06T08:43:26+00:00

Total noob here. This is my first c# attempt, its a console application that

  • 0

Total noob here. This is my first c# attempt, its a console application that simulates a drinking game called ‘Left Right Center’. In the console I receive 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 

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

            }
        }
    }

I have no idea what the problem is or why, and my googling have proven more use confusing than helpful.

For those who are curious, I have little experiment working now

http://pastebin.com/jxCCW2cd

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:43:29+00:00Added an answer on June 6, 2026 at 8:43 am

    This line

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

    declares an array that allows the storage of 2 objects of the Dice class, but each value in this array is still null.

    You need to create a Dice on 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 the rattle Dice is null.

    EDIT: Looking at your code I have found a problematic situations
    In the Roll method, you recreate the Random generator and this is no good for randomness. (See the accepted answer in this question)
    Last, theDice array could be created and initialized in the same way you already do for thePlayers array

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

    This is a complete revision of your 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;             
        } 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey! Total CakePHP noob here. Updated at bottom / This is sort of a
First timer here, and total noob when it comes to PHP and JavaScript. I
Total noob here with javascript. I'm trying to alter a function. This is the
Total noob question, but here. CSS .product__specfield_8_arrow { /*background-image:url(../../upload/orng_bg_arrow.png); background-repeat:no-repeat;*/ background-color:#fc0; width:50px !important; height:33px
I'm a total noob when it comes to C but i found this curl
Ok.. total noob question. I always thought that when I saw a URL like
I am a total Perl noob and trying to figure out if this thing
Total noob question here, but not finding the answer via search. What is the
well...just about all I needed to say...lookie here, I'm a total noob in web
I am a total noob on this issue, and I wonder if I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.