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 9200849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:51:39+00:00 2026-06-17T22:51:39+00:00

I’m making a card game and I’m getting the error that n must be

  • 0

I’m making a card game and I’m getting the error that n must be positive. I did some research and it means that (cards.size) is equal or less than 0 I believe, but I don’t understand how to make my code work, something must be wrong.

Code:

public class Deck
{
public ArrayList <Card> cards;
Deck()
{
    cards = new ArrayList<>();
    for (int a = 0; a < 52; a++)
    {           
            cards.add(new Card(a));         
    }
}

public Card PlayerCardDraw ()
{
    Random generator = new Random ();
    int index = generator.nextInt (cards.size ());
    return cards.remove (index);

}

How can I fix my array list so i don’t get this error? it relates back to Card so I’ll post that code too, I know something isn’t right in my Card class but I don’t know if that’s the problem or not.

public class Card
{
int c = 52;
int cardpath[] = new int[c];

Card ()
{

}


public Card(int c)
{
    this.c = c;
}

public int getCardPath()
{
return cardpath[c];
}   
}

Error message:

java.util.Random.nextInt(Unknown Source) at Cards.Deck.PlayerCardDraw(Deck.java:21)
line 21 is int index = generator.nextInt (cards.size ());

EDIT: I did what Nankumar Tekale said and it’s saying what you guys predicted: It’s drawing more than 52 cards. What I don’t understand is the error is popping up at

for (int i = 0 ; i < 4 ; i++)
    {
        C = deck.P1CardDraw ();
        card [cardNum].draw (i*75+100, 400); //Error line
        cardNum++;                   
    }

my P1CardDraw() class

public ArrayList < Card > p1Hand;
public ArrayList < Card > P1CardDraw ()
{

    p1Hand = new ArrayList < > ();
    p1Hand.add (PlayerCardDraw ());
    return p1Hand;
}
  • 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-17T22:51:41+00:00Added an answer on June 17, 2026 at 10:51 pm

    Well looking at your Deck class, You have intialized cards in constructor so there should not be such an exception(as cards size is 52).

    But one thing is possible to get an exception which is cards is declared public and you may have modified it outside class directly. So arraylist cards is of size 0 and you get IllegalArgumentException exception for your method Random.nextInt(int).

    Make cards private.

    If you have withdrawn all cards then size of arraylist would become 0, which may cause an exception. Add check for 0 as :

    public Card PlayerCardDraw ()
    {
        Random generator = new Random ();
        if(cards.size() > 0) {
            int index = generator.nextInt (cards.size());
            return cards.remove (index);
        } else {
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this

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.