c# - Object reference not set to an instance of an object - Black Jack Game -


I'm a bit of a programming novice and I'm making a black jack game, but I'm really struggling when I get this message from the hit function, then add one card to the player's hand with my fired deck,

object.

I know that the solution really is really clear, but can you please help? The problem is below deck class hit () function

/ * main square * /

  Using the system; Using System.Collections.Generic; Using System.Text; Namespace BlackJackGameX {public class main class {public static zero main (string [] arg) {deck card = new deck (); Hand player hand = new hand (card); Console WrightLine ("Welcome to Black Jack \ n \ nPress Enter to get started"); Console Readline (); Cards.Hit (); PlayerHand.PrintHand (); }}}   

/ * Card class * /

  using the system; Using System.Collections.Generic; Using System.Text; Namespace Black Jack GameX {Public Swan, Hearts, Clubs, Diamonds} Public Face (Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King) Public Face Card {public suit card suit; Public face value card value; Public IT iValue; Public Cards (Suits CardSuite, Face Value Card Value, Int ivalue) {CardSuit = cardSuit; Card Values ​​= Cardgulls; IV = ivalue; } Public card () {}}}   

/ * hand class * /

  using System.Collections.Generic; Using System.Text; Named Location Black Jack Game {Public Class Hand {Public List & lt; Cards & gt; PlayerHand; Private deck carded deck = new deck (); Public hand (deck card) {cardDeck = card; } Public Zero printhand (for ((int i = 0; i.m. player player; ++ i) {console. [I] .cardSuit;; (i & lt; PlayerHand.Count) {Console.WriteLine ("& amp;");}} console .readline ();}}}   

/ * Deck orbit * /

 using  system; using System.Collections.Generic; using System.Text; name space black jack game x {public square deck (hand Player hands; Random RNM = new random (); List of & lt; cars E & gt; cards; public IT iValue1 = 11; public IT iValue2 = 2; public IT iValue3 = 3; public IT iValue4 = 4; public Int iValue5 = 5; public Int iValue6 = 6; public Int iValue7 = 7; public Int iValue8 = 8; public Int iValue9 = 9; public Int iValue10 = 10; public IT iValue11 = 10; public IT iValue12 = 10; public IT iValue13 = 10; public deck () {card = Nudek ();} private list & lt ; Card & Nudec () {var AllSuits = New Suit [] {Suit. Speedus, Suite. Heights, suits. Club, suit Dims}; Var AllFaces = new FaceValue [] (FaceValue.Ace, FaceValue.Two, face Valley. Three, face Valikfur, face Valukfiv, face Valle. Coin, face Valley. Seventh, face Valukiit, face Valley. Nain Face Velukn, face Valley. Jack, face value. Quinn, face value. King}; Var AllValues ​​[= new int] (iValue1, iValue2, iValue3, iValue4, iValue5, iValue6, iValue7, iValue8, iValue9, iValue10, iValue11, iValue12, iValue13}; list & lt; card & gt; MyDeck = new list & lt; card & gt; () for; (Int I = 0; i & lt; = 3; i ++ } {For (int j = 0; j and lt; = 12; j ++) {card newcard = new car E (Olsuts [i], Olfes [J], Olwal [j]); myDeck.Add (newCard);}} List & lt; card & gt; ShuffledDeck = new List & lt; card & gt; (); while (MyDeck.Count> 0) {int c = myDeck.Count; Int n = rNumber.Next (0, c); // var will be translated into an int using the necessary card from var value = myDeck [n] Will be used to take; shuffledDeck.Add (value); myDeck.Remove (value);} Change stinky deck;} Public Zero print () (for (int i = 0; I & lt; = 51; I ++) {console. Virusline ("You have a" + (card [i]. Cardvalue) + "of" + (card [i] .cardSuit)); Console Readline (); }} Hit public Zero () {playerHand.PlayerHand.Add (Card [0]); Cards.Remove (Card [0]); / * Var tmp = card [0]; Cards.Remove (tmp); Returns TMP; * /}}    

public list & lt; Cards & gt; PlayerHand; is never assigned to its hand class and always null . In

hits you call playerHand.PlayerHand.Add - playerHand.PlayerHand is null < / Code>, therefore code add result in NullReferenceException .

An exception has been thrown where debugging through function should clearly show such issues - you can see the variables used and see which values ​​are expected and who do not

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -