Debugging Blackjack program? (Java) -


i trying write blackjack program user bets against cpu dealer.
the problem having have written classes card, deck, , dealer, when try initialize new dealer , new deck error:

exception in thread "main" java.lang.nullpointerexception @ blackjack.deck.<init>(deck.java:12) @ blackjack.blackjack.main(blackjack.java:10) 

here class card:

 class card {       private int rank;       private int suit;       private int value;       private static string[] ranks = {"joker","ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"};       private static string[] suits = {"clubs","diamonds","hearts","spades"};        card(int suit, int values)       {            this.rank=values;            this.suit=suit;            if(rank>10)            {                 value=10;            }            else                 value=rank;       }       public string tostring()       {       return ranks[rank]+" of "+suits[suit];       }       public int getrank()       {       return rank;       }       public int getsuit()       {            return suit;       }       public int getvalue()       {            return value;       }       public void setvalue(int set)       {            value = set;       }  } 

my class deck:

package blackjack; import java.util.arraylist; import java.util.random;   class deck {       private arraylist<card> deck;       deck()       {            for(int i=0; i<4; i++)            {                 for(int j=1; j<=13; j++)                 {                      deck.add(new card(i,j));                 }            }       }       public void shuffle()       {            random random = new random();            card temp;            for(int i=0; i<200; i++)            {                 int index1 = random.nextint(deck.size()-1);                 int index2 = random.nextint(deck.size()-1);                 temp = deck.get(index2);                 deck.set(index2, deck.get(index1));                 deck.set(index1, temp);            }       }       public card drawcard()       {            return deck.remove(0);       }  } 

my class dealer:

package blackjack; import java.util.arraylist; import java.util.arrays; class dealer { arraylist<card> hand; private int handvalue=0; private card[] ahand; dealer(deck deck) {     hand = new arraylist<>();     ahand = new card[]{};     for(int i=0; i<2; i++)     {         hand.add(deck.drawcard());     }     ahand = hand.toarray(ahand);     for(int i=0; i<ahand.length; i++)     {         handvalue += ahand[i].getvalue();         if(ahand[i].getvalue()==1 && handvalue<12)         {             handvalue=handvalue+10;         }     } } public string showfirstcard() {     card[] firstcard = new card[]{};     firstcard = hand.toarray(firstcard);     return firstcard[0].tostring(); } public void hit(deck deck) {     hand.add(deck.drawcard());     ahand = hand.toarray(ahand);     handvalue = 0;     for(int i=0; i<ahand.length; i++)     {         handvalue += ahand[i].getvalue();         if(ahand[i].getvalue()==1 && handvalue<12)         {             handvalue=handvalue+10;         }     } } public boolean wantstohit() {     if(handvalue<17)     {         return true;     }     return false; } public boolean hasblackjack() {     if(hand.size()==2 && handvalue==21)     {         return true;     }     return false; } public string showhand() {     ahand = hand.toarray(ahand);     string hands="";     for(int i=0; i<ahand.length-1; i++)     {         hands = ahand[i].tostring()+", ";     }     hands = hands + ahand[ahand.length-1].tostring();     return hands; } public int gethandvalue() {     return handvalue; } 

}

and main method:

 public class blackjack {  private static int cash;  public static void main(string[] args){       system.out.println("hi! name?");       scanner scan = new scanner(system.in);       string name = scan.nextline();       system.out.println("hello, "+name+" lets plays blackjack!");       deck deck = new deck();       deck.shuffle();       dealer dealer = new dealer(deck);       system.out.println(dealer.showhand());       system.out.println(dealer.gethandvalue());       }  } 

any appreciated. thanks!

in deck attribute deck never initialized , null. since deck null line: deck.add(new card(i,j)); throw nullpointerexception. create deck before for-loops solve problem

deck() {     deck = new arraylist<>();      for(int i=0; i<4; i++)     {         for(int j=1; j<=13; j++)         {             deck.add(new card(i,j));         }       } } 

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -