构造函数将三个数组作为参数,在构造对象时生成NPE

时间:2015-04-13 19:11:34

标签: java arrays nullpointerexception

如果出现问题,请原谅我,因为这是我的第一篇文章。 这是我给出的问题:

Deck构造函数 - 此构造函数接收三个数组作为参数。数组包含套牌中每张卡的等级,套装和点值。构造函数创建一个ArrayList,然后创建指定的卡并将它们添加到列表中。 例如,如果ranks = {"A", "B", "C"}suits = {"Giraffes", "Lions"}, 和values = {2,1,6},构造函数将创建以下卡片: ["A", "Giraffes", 2]["B", "Giraffes", 1]["C", "Giraffes", 6]["A", "Lions", 2]["B", "Lions", 1]["C", "Lions", 6] 并将它们中的每一个添加到cards。然后将参数大小设置为cards的大小,在此示例中为6。

这就是我所做的:

Deck构造函数 -

public Deck(String[] ranks, String[] suits, int[] values) {
     for (int i = 0; i < suits.length; i++) {
         for (int j = 0; j < ranks.length; j++) {
             Card cardX = new Card(ranks[j], suits[i], values[j]);
             cards.add(cardX); //this is the line that gives the error
             size++;
         } 
     }
 }

完成未经编辑的Deck课程 -

import java.util.List;
import java.util.ArrayList;

/**
 * The Deck class represents a shuffled deck of cards. It provides several
 * operations including initialize, shuffle, deal, and check if empty.
*/
public class Deck {

/**
 * cards contains all the cards in the deck.
 */
private List<Card> cards;

/**
 * size is the number of not-yet-dealt cards. Cards are dealt from the top
 * (highest index) down. The next card to be dealt is at size - 1.
 */
private int size;

/**
 * Creates a new <code>Deck</code> instance.<BR>
 * It pairs each element of ranks with each element of suits, and produces
 * one of the corresponding card.
 *
 * @param ranks is an array containing all of the card ranks.
 * @param suits is an array containing all of the card suits.
 * @param values is an array containing all of the card point values.
 */
public Deck(String[] ranks, String[] suits, int[] values) {
    /*
     Deck constructor — This constructor receives three arrays as parameters. The arrays contain
     the ranks, suits, and point values for each card in the deck. The constructor creates an
     ArrayList, and then creates the specified cards and adds them to the list.

     For example, if ranks = {"A", "B", "C"}, suits = {"Giraffes", "Lions"},
     and values = {2,1,6}, the constructor would create the following cards:

     ["A", "Giraffes", 2], ["B", "Giraffes", 1], ["C", "Giraffes", 6],
     ["A", "Lions", 2], ["B", "Lions", 1], ["C", "Lions", 6]

     and would add each of them to cards. The parameter size would then be set to the size of
     cards, which in this example is 6.
     Finally, the constructor should shuffle the deck by calling the shuffle method. Note that you
     will not be implementing the shuffle method until Activity 4
     */
    for (int i = 0; i < suits.length; i++) {
        for (int j = 0; j < ranks.length; j++) {
            Card cardX = new Card(ranks[j], suits[i], values[j]);
            cards.add(cardX);
            size++;
        } 
    }
     //cards.shuffle();
}

/**
 * Determines if this deck is empty (no undealt cards).
 *
 * @return true if this deck is empty, false otherwise.
 */
public boolean isEmpty() {
    /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
    return cards.size() == 0;
}

/**
 * Accesses the number of undealt cards in this deck.
 *
 * @return the number of undealt cards in this deck.
 */
public int size() {
    /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
    return size;
}

/**
 * Randomly permute the given collection of cards and reset the size to
 * represent the entire deck.
 */
public void shuffle() {
    /* *** TO BE IMPLEMENTED IN ACTIVITY 4 *** */
}

/**
 * Deals a card from this deck.
 *
 * @return the card just dealt, or null if all the cards have been
 * previously dealt.
 */
public Card deal() {
    /* *** TO BE IMPLEMENTED IN ACTIVITY 2 *** */
    /* 
    Algorithm 2: It would be more efficient to leave the cards in the list. 
Instead of removing the
card, simply decrement the size instance variable and then return the card at size. In this algorithm, the size instance variable does double duty; it determines which card to “deal” and it also represents how many cards in the deck are left to be dealt. This is the algorithm that you should implement.
    */
    size--;
    return cards.get(size);    
}

/**
 * Generates and returns a string representation of this deck.
 *
 * @return a string representation of this deck.
 */
@Override
public String toString() {
    String rtn = "size = " + size + "\nUndealt cards: \n";

    for (int k = size - 1; k >= 0; k--) {
        rtn = rtn + cards.get(k);
        if (k != 0) {
            rtn = rtn + ", ";
        }
        if ((size - k) % 2 == 0) {
            // Insert carriage returns so entire deck is visible on console.
            rtn = rtn + "\n";
        }
    }

    rtn = rtn + "\nDealt cards: \n";
    for (int k = cards.size() - 1; k >= size; k--) {
        rtn = rtn + cards.get(k);
        if (k != size) {
            rtn = rtn + ", ";
        }
        if ((k - cards.size()) % 2 == 0) {
            // Insert carriage returns so entire deck is visible on console.
            rtn = rtn + "\n";
        }
    }

    rtn = rtn + "\n";
    return rtn;
}
}

来自Card类的Card构造函数

public Card(String cardRank, String cardSuit, int cardPointValue) {
    rank = cardRank;
    suit = cardSuit;
    pointValue = cardPointValue;
}

Deck -

中构建Main对象的调用
String[] ranks1 = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
String[] suits1 = {"Spades", "Clubs", "Hearts", "Diamonds"};
int[] values1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

Deck deck1 = new Deck(ranks1, suits1, values1); //gives error, tosses error back to Deck constructor

我哪里错了?

0 个答案:

没有答案