Метод pop в стеке вызывает исключение нулевого указателя - PullRequest
2 голосов
/ 12 марта 2012

Я только что реализовал цикл для заполнения руки игрока (очереди) картами (из колоды), но я сталкиваюсь с исключением NullPointerException в LinkedStack.pop (). Я не изменил предоставленный код LinkedStack и проверил, что колода заполнена элементами, поэтому я не могу сразу увидеть, откуда исходит исключение NullPointerException. Ниже приведены мои сегменты кода и вывод ошибок. Если вам нужен какой-либо другой код, просто дайте мне знать.

(SimpleUnoGame.java)

45    LinkedQueue<UnoCard> cardHand = new LinkedQueue<UnoCard>();
46    //draw cards for player
47    for(int j = 0; j < INITIAL_CARDS; j++) {
48        cardHand.enqueue(faceDownCards.pop());
49    }
50    newPlayer.setCards(cardHand);
51    playerCollection.enqueue(newPlayer);

(LinkedStack.pop ())

43    T result = top.getElement();
44    top = top.getNext();
45    count--;
46    return result;

(Exception)

Exception in thread "main" java.lang.NullPointerException
    at LinkedStack.pop(LinkedStack.java:43)
    at SimpleUnoGame.<init>(SimpleUnoGame.java:48)

Вот полный код в соответствии с запросом:

(LinkedStack.java)

/**
 * Represents a linked implementation of a stack
 */

public class LinkedStack<T> implements StackADT<T> {
    //indicates number of elements stored
    private int count;  
    //pointer to top of stack
    private LinearNode<T> top; 

    /**
     * Creates an empty stack
     */
    public LinkedStack() {
        count = 0;
        top = null;
    }

    /**
     * Adds the specified element to the top of this stack
     * @param element element to be pushed on stack
     */
    public void push(T element) {
        LinearNode<T> temp = new LinearNode<T>(element);

        temp.setNext(top);
        top = temp;
        count++;
    }

    /**
     * Removes the element at the top of this stack and returns a reference to it
     * Throws an EmptyCollectionException if the stack is empty
     * @return T element from top of stack
     * @throws EmptyCollectionException on pop from empty stack
     */
    public T pop() throws EmptyCollectionException {
        if (isEmpty()) {
            throw new EmptyCollectionException("Stack");
        }

        T result = top.getElement();
        top = top.getNext();
        count--;

        return result;
    }

    /**
     * Returns a reference to the element at the top of this stack
     * Throws an EmptyCollectionException if the stack is empty
     * (the element is not removed from the stack)
     * @return T element on top of stack
     * @throws EmptyCollectionException on peek at empty stack  
     */
    public T peek() throws EmptyCollectionException {
        if (isEmpty()) {
            throw new EmptyCollectionException("Stack"); 
        }

        return top.getElement();
    }

    /**
     * Returns true if this stack is empty and false otherwise
     * @return boolean true if stack is empty
     */
    public boolean isEmpty() {
        if (count == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Returns the number of elements in this stack
     * @return int number of elements in this stack
     */
    public int size() {
        return count;
    }

    /**
     * Returns a string representation of this stack
     * @return String representation of this stack
     */
    public String toString() {
        String output = "";
        T result;

        for (int i = 0; i < this.size(); i++) {
            result = top.getElement();
            output = output + result;
            top = top.getNext();
        }

        return output;
    }
}

(SimpleUnoGame.java)

public class SimpleUnoGame {

    private final int INITIAL_CARDS = 7;
    private LinkedQueue<UnoPlayer> playerCollection = new LinkedQueue<UnoPlayer>();
    private LinkedStack<UnoCard> faceUpCards = new LinkedStack<UnoCard>();
    private LinkedStack<UnoCard> faceDownCards = new LinkedStack<UnoCard>();
    private int cardCount = 0;

    public SimpleUnoGame(int numberOfPlayers, int highestRank) {
        //create cards and add to faceDownCards
        for(int i = 1; i <= highestRank; i++) {
            for(int j = 0; j <= 1; j++) {
                //blue cards
                UnoCard cardB = new UnoCard('B', i);
                faceDownCards.push(cardB);
                //green cards
                UnoCard cardG = new UnoCard('G', i);
                faceDownCards.push(cardG);
                //red cards
                UnoCard cardR = new UnoCard('R', i);
                faceDownCards.push(cardR);
                //yellow cards
                UnoCard cardY = new UnoCard('Y', i);
                faceDownCards.push(cardY);
            }
        }

    System.out.println("Cards: " + faceDownCards.toString());  //debug check

    //shuffle cards randomly
    shuffleCards(faceDownCards);

    System.out.println("Cards: " + faceDownCards.toString());  //debug check

    //create players and add to playerCollection
    for (int i = 0; i < numberOfPlayers; i++) {
        //ask for name
        String name = "Taylor";

        UnoPlayer newPlayer = new UnoPlayer(name);
        LinkedQueue<UnoCard> cardHand = new LinkedQueue<UnoCard>();

        //draw cards for player
        for(int j = 0; j < INITIAL_CARDS; j++) {
            cardHand.enqueue(faceDownCards.pop()); //PROBLEM OCCURS HERE
        }

        newPlayer.setCards(cardHand);
        playerCollection.enqueue(newPlayer);
    }

    System.out.println("There are " + numberOfPlayers + " players in the game.");
    System.out.println(playerCollection.toString());  //debug check

    //draw one face up card
    faceUpCards.push(faceDownCards.pop());
}

1 Ответ

2 голосов
/ 12 марта 2012

Ваша переменная top равна нулю.Как вы его инициализируете?

Из вашего примера кода кажется, что вы устанавливаете top в null при построении вашего объекта, а затем устанавливаете его в значение, отличное от null, когда вы нажимаете элемент.

Если это так, то кажется, что вы не вставили что-то в LinkedStack.

Хотя я должен признать, что мне трудно выяснить точную проблему, поскольку вы предоставили только битыВаш код и его вроде бы не имеет особого смысла.

РЕДАКТИРОВАТЬ

Метод toString () изменяет поле top , которое предполагаетсядержать голову в своей очереди.Вызов метода toString не должен иметь побочных эффектов.Измените его следующим образом:

public String toString() {
    String output = "";
    T result;
    LinearNode<T> tempHead = top;

    for (int i = 0; i < this.size(); i++) {
        result = tempHead.getElement();
        output = output + result;
        tempHead = tempHead.getNext();
    }

    return output;
}

Я не знаю, кто написал этот класс для вас, но это ошибка новичка.Вызывая toString (), вы фактически очищаете свою очередь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...