Deck类中的java.lang.NullPointerException

时间:2016-04-24 23:42:32

标签: java nullpointerexception

    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;
            for (j = 0; j < 2; j++) {
                int i = 0;
                for (Color c : Color.values()) {
                    for (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;

      }
}

我在Deck类中得到了一个nullpointerexception,它说toString大约一半,我不太清楚为什么。我正在为我的基于java的类制作一个uno游戏,并且我正在尝试创建卡片。谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

在下面的代码中

public void load(){
  int j = 0;
  for (j = 0; j < 2; j++) {
    int i = 0;
    for (Color c : Color.values()) {
      for (Value v : Value.values())
        cards[i++] = new Card(c, v);
    }
  }
}

您在i for循环中将j重置为0。您打算做什么(我假设)在循环外声明为0,以便为每张卡制作两份副本。为此,请将其更改为以下内容:

public void load(){
  int j = 0;
  int i = 0;
  for (j = 0; j < 2; j++) {
    for (Color c : Color.values()) {
      for (Value v : Value.values())
        cards[i++] = new Card(c, v);
    }
  }
}

正如旁注,我认为你也想要120张卡,而不是112张。15种卡类型* 4种颜色*每张卡2张= 120。

答案 1 :(得分:0)

在您的代码中:

 public void load(){
        int j = 0;
        for (j = 0; j < 2; j++) {
            int i = 0;  //<-- i is reset during the next loop
            for (Color c : Color.values()) {
                for (Value v : Value.values())
                    cards[i++] = new Card(c, v);
            }

        }

我在第二次循环时重置为0。这意味着你的一半牌[]不会被初始化。

 public void load(){
        int j = 0;
        int i = 0; // <-- move i = 0 here
        for (j = 0; j < 2; j++) {
            //int i = 0;  //<-- i is reset during the next loop
            for (Color c : Color.values()) {
                for (Value v : Value.values())
                    cards[i++] = new Card(c, v);
            }

        }

同样,112 semms也有空间。

public static final int CARDSOFDECK = 120;

会更好。