Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7617149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:04:40+00:00 2026-05-31T03:04:40+00:00

I have just implemented a loop to populate a player’s hand (a queue) with

  • 0

I have just implemented a loop to populate a player’s hand (a queue) with cards (from a stack), but I am encountering a NullPointerException at LinkedStack.pop(). I haven’t altered the provided LinkedStack code and I have checked that the deck is populated with elements, so I cannot immediately see where the NullPointerException is coming from. Below are my code segments and error output. If you require any other code, just let me know.

(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)

Here is the complete code, as requested:

(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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:04:41+00:00Added an answer on May 31, 2026 at 3:04 am

    Your top variable is null. How do you initialize it?

    From your code sample it seems that you set top to null upon construction of your object and then only set it to something non-null when you push an item.

    If that’s the case, then it seems that you have not pushed something into the LinkedStack.

    Although i must admit i have trouble figuring out the exact problem since you have only supplied bits of your code and it sort of doesn’t make much sense.

    EDIT

    The method toString() modifies the top field which is supposed to hold the head of your queue. Calling the toString method should have no side-effects. Change it to this :

    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;
    }
    

    I don’t know who wrote this class for you but it’s a rookie mistake. By calling toString() you are effectively emptying your queue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just implemented WMD for my editor in an ASP.NET app. The problem
I am developing a Silverlight app using VS2008 Express. I have just implemented a
I'm just learning Qt with C++. I have successfully implemented signals and slots to
We're just getting into MVVM in WPF. We have implemented our ViewModels with 'strongly
i have implemented a tile based layer in my game, but after the first
I just finished reading this post: https://developer.yahoo.com/performance/rules.html#flush and have already implemented a flush after
I have just noticed that a multidimensional array in C# does not implement IEnumerable<T>
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have just started playing with ASP.NET MVC and have stumbled over the following situation.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.