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

  • SEARCH
  • Home
  • 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 7867353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:41:26+00:00 2026-06-03T00:41:26+00:00

Trying to create the card game UNO in java. When a player plays a

  • 0

Trying to create the card game “UNO” in java. When a player plays a card, it should be removed from the hand with the other elements shifting to the left. It takes an int n as the parameter, which refers to the card being discarded. The method should change the cards array that I have specified as a field of the class. It’s an array of objects which are the cards, or the players hand. When ran, it produces a nullPointerException. I know why the error is occurring, im just not sure how to fix it. I’m also trying to avoid the use of Array Lists. It also returns the card that is being discarded so it can be printed. Thanks.

public Card removeCardFromHand(int n)
{
    Card c = cards[n];
    Card[] tempCards = new Card[cards.length - 1];
    for(int i = 0; i < n; i++)
    {
        tempCards[i] = cards[i];
    }
    for(int i = n; i < cards.length; i--)
    {
        tempCards[n] = cards[n + 1];
    }
    cards = tempCards;
    return c;
} 

Error Code:

java.lang.ArrayIndexOutOfBoundsException: 7

at Player.removeCardFromHand(Player.java:86)
at BUno.executeOnePlay(BUno.java:112)
at BUno.play(BUno.java:70)
at BUno.main(BUno.java:186)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:271)

It’s occurring because, in this case, the player had 7 cards. When the 7th one was removed, that 7th index was then empty. I wrote a similar method for adding a card when a player has to draw a card, which worked flawlessly. I am practicing for an upcoming exam, which doesn’t cover array lists or vectors, so it’s useless for me to use them.

  • 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-06-03T00:41:27+00:00Added an answer on June 3, 2026 at 12:41 am
    for(int i = n; i < cards.length; i--)
    {
        tempCards[n] = cards[n + 1];
    }
    

    What is that? 🙂

    Three immediate problems. The first is that you’re using n in the array indexes within the loop rather than the correct i.

    The second is that, even when you fix that, you’re going to go beyond the end of the array.

    The third is that you should be incrementing i rather than decrementing it. Decrementing it means that the loop will run forever since i will always be less than cards.length. And, by forever, I mean right up to the point where you start trying to do something with cards[-1] 🙂

    Instead, you should try:

    for (int i = n; i < cards.length - 1; i++)
        tempCards[i] = cards[i + 1];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to create a board game generator in ruby which takes images from
I'm trying to create image from SQLite database in Java Swing application. But it
I am trying to develop an UNO card game, and now I reach to
Using Rebol/View 2.7.7, I'm trying to create a card game based on Nick's Rebol
I'm trying to develop a card game with Flex, and I was wondering if
i am trying to create sd card on emulator. in devtools in terminal emulator
I am trying to create a Music Player App in J2ME. I want to
The documentation for MFMessageComposeViewController says you should check [MFMessageComposeViewController canSendText] before trying to create
In a Russian card game I'm trying to keep statistics of how often a
I'm trying to model a card game in order to learn Rails. This is

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.