#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    C# learning project: horse racingg


    Hi all,

    I am learning C#, I am working on Lab1 in the Head first C# book. The purpose of the lab is to create a horse racing simulator. The method below is in a guy class. I kept getting the error, NullReference Exception Unhandled, when I tried to use the GetDescription method from the Bet class. Bascially, I have a Guy class and a Bet class, and a method in the Guy class call a method in the Bet class, but that doesn't work. I tried to create an object, but that doesn't solve the problem. Can someone give me an idea? I am new to C# and object oriented

    public void UpdateLabels() {
    // Set my label to my bet's description, and the label on my
    // radio button to show my cash ("Joe has 43 bucks")

    MyLabel.Text = MyBet.GetDescription();
    MyRadioButton.Text = this.Name + " has " + this.Cash + " bucks";


    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    185
    Rep Power
    82
    It indicates that you are trying to access member fields, or function types, on an object reference that points to null. It indicates a flaw in the code.

    IMHO, it's just impossible to pinpoint the source of your problem with the code snippet provided above. You'll have to post all the source code for review.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by BobS0327
    It indicates that you are trying to access member fields, or function types, on an object reference that points to null. It indicates a flaw in the code.

    IMHO, it's just impossible to pinpoint the source of your problem with the code snippet provided above. You'll have to post all the source code for review.
    How do I attach my code here?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    13
    Rep Power
    0
    Just copy your code inbetween [*code][/*code]

    with no asterisks
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Lab1Race
    {
        public class Guy
        {
            public string Name; // The guy's name
    
    
            public Bet MyBet; // An instance of Bet that has his bet
            public int Cash; // how much cash he has
    
            // The last two fields are the guy's GUI controls on the form
            public RadioButton MyRadioButton; // My RadioButton
            public Label MyLabel; // My Label
    
    
            public void UpdateLabels() {
                // Set my label to my bet's description, and the label on my
                // radio button to show my cash ("Joe has 43 bucks")
    
    
                MyLabel.Text = MyBet.GetDescription();
                MyRadioButton.Text = this.Name + " has " + this.Cash + " bucks";
    
                 
            }
    
            public void ClearBet() {
                MyBet.Amount = 0;
            
            } // Reset my bet so it's zero
    
            public bool PlaceBet(int Amount, int Dog) {
                // Place a new bet and store it in my bet field
                //return true if the Guy had enough money to bet
                MyBet = new Bet();
                MyBet.Bettor = this;
                MyBet.Amount = Amount;
                MyBet.Dog = Dog;
                return this.Cash < Amount;
      
       
            }
    
            public void Collect(int Winner) {
    
                this.Cash += MyBet.PayOut(Winner);
            
            
            } // Ask my bet to pay out
        }
    }
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Lab1Race
    {
        public class Bet
        {
            public int Amount; // The amount of cash that was bet
            public int Dog; // the number of the dog the bet is on
            public Guy Bettor; // The guy whom placed the bet
    
            
            public string GetDescription()
            {
    
                if (this.Amount > 0)
                {
                    return Bettor.Name + " hasn't placed a bet";
                }
                else
                {
                    return Bettor.Name + " bets " + Amount + " on dog #" + Dog;
                }
    
    
                //Return a string that says who palced the bet, how much
                // cash was bet, and which dog he bet on ("Joe bets 8 on
                // dog #4").  If the amount is zero, no bet was placed
                // ("Joe hasn't placed a bet").
            }
    
            public int PayOut(int Winner)
            {
                //The parameter is the winner of the race.  If the dog won,
                // return the amount bet.  Otherwise, return the negative of
                // the amount bet.
                if (this.Dog == Winner)
                {
                    return this.Amount;
                }
                else
                {
    
                    return -1 * this.Amount;
                }
            }
        }
    }
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    185
    Rep Power
    82
    It appears as though your Name string variable is not initialized anywhere in your code. That may be the source of your problem.

    Has your code generated a stack trace? If so, provide the details of the trace if initializing Name does not solve the problem.

IMN logo majestic logo threadwatch logo seochat tools logo