java.lang.NullPointerException in the Deck class -
public class deck { public static final int cardsofdeck = 112; private card[] cards = new card[cardsofdeck]; public static void main(string[] args){ deck deck = new deck(); deck.load(); deck.show(); } public void show(){ for(card c: cards) system.out.println(c.tostring()); } public void load(){ int j = 0; (j = 0; j < 2; j++) { int = 0; (color c : color.values()) { (value v : value.values()) cards[i++] = new card(c, v); } } } } public class card { private color color; private value value; public card(color c, value v) { color = c; value = v; } public string tostring() { return color + " " + value; } } public enum color { red("red"), yellow("yellow"), blue("blue"), green("green"); private final string cardcolor; private color(string cardcolor){ this.cardcolor = cardcolor; } public string getcolor(){ return cardcolor; } } public enum value { zero(0), one(1), two(2), three(3), four(4), five(5), six(6), seven(7), eight(8), nine(9), skip(10), draw2(11), reverse(12), wild(13), wilddraw4(14); private final int cardvalue; private value(int cardvalue){ this.cardvalue = cardvalue; } public int getvalue(){ return cardvalue; } }
i getting nullpointerexception in deck class around says tostring halfway down, , not sure why. making uno game java based class , trying cards created. thank help!
in code below
public void load(){ int j = 0; (j = 0; j < 2; j++) { int = 0; (color c : color.values()) { (value v : value.values()) cards[i++] = new card(c, v); } } }
you reset i
0 within j
loop. intend (i assume) declare 0 outside loop in order make 2 copies of each card. that, change below:
public void load(){ int j = 0; int = 0; (j = 0; j < 2; j++) { (color c : color.values()) { (value v : value.values()) cards[i++] = new card(c, v); } } }
just side note, think want have 120 cards, not 112. 15 card types * 4 colors * 2 of each card = 120.
Comments
Post a Comment