04-01-2013 09:46 AM
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. See attached for codes.
04-04-2013 02:27 PM
@jyang72211 wrote:
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. See attached for codes.
With the way you have written your code, I would try
if (People[guyIndex].PlaceBet((int)betAmountBox.Value, (int)dogNumberBox.Value))
{
//Bet MyBet = new Bet() { Amount = (int)betAmountBox.Value, Dog = (int)dogNumberBox.Value, Bettor = People[guyIndex] };
People[guyIndex].MyBet.Amount = (int)betAmountBox.Value;
People[guyIndex].MyBet.Dog = (int)dogNumberBox.Value;
People[guyIndex].MyBet.Bettor = People[guyIndex];
People[guyIndex].UpdateLabels();
}