-6

I'm trying to create a card game with two players; you and the computer. I'm attempting to form a main ArrayList where each row holds a ArrayList of cards objects. Each row holds Diamonds, Clubs, Hearts and Spaces; therefore the main ArrayList has a maximum of four rows.

The card object is Cards(int cardNo, Rank rank, Suit suit, BufferedImage tempcardImage, boolean isFaceup, boolean isCompCard).

Before I added the following code the program worked well and produced the 5 cards layout[program output 1 of 5][1[]]1.The 5 layouts are for Diamonds,Clubs.Hearts,Spades and No Trumps as the trumps. Below the 4 x 5 grid of cards is another, making 52 cards. The player who wins a trick can look at the six cards.

I'm trying to write a procedure where the computer picks a card to lay down. Hence I wrote the following code.

The following code shows a number of errors when it tries to setup the ArrayLists. Any help would be appreciated, since this is my first question stackoverflow.

ArrayList<ArrayList<Cards>> suitsCards = new ArrayList<ArrayList<Cards>>();

for(int i=3;i>-1;i--){
    suitsCards.add(i,new ArrayList<Cards>());
}

for(int i=3;i>-1;i--){
    for(int k=0;k<combinedCardsList.size();k++){
         if(combinedCardsList.get(k).suitValue()==i){
              suitsCards.get(i).add(combinedCardsList.get(k));
         }
     }
} 

for(int i=0;i<4;i++){
    for(int k=0;k<suitsCards.get(i).size();i++){
        System.out.print(" "+suitsCards.get(i).get(k).suitValue());
    }
    System.out.println();
}

Thee errors I get are as follows:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 3, Size: 0 at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:665) at java.util.ArrayList.add(ArrayList.java:477) at newcards.GUI.(GUI.java:443) at newcards.guiIntro.legOfGame(guiIntro.java:168) at newcards.guiIntro$2.actionPerformed(guiIntro.java:107) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6533) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

  • 1
    Add the detailed errors – Gaurav Srivastav Jun 19 '18 at 10:29
  • 1
    What is the question? – MartinByers Jun 19 '18 at 10:32
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the [edit] link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Jun 19 '18 at 10:35
  • Show a [mcve] including the `Cards` class (if its relevant) and don't forget to show the full error message, including stack trace. Also don't forget to tell us what the code is supposed to do. – Zabuzard Jun 19 '18 at 10:35
  • 2
    And the real answer here: read each error message carefully. Maybe comment out some code, to have **less** errors to start with. Then put the core of the error message into a search engine. Most likely, all your errors have zillions of online resources explaining them. Because, honestly: this site is not an error message explanation service. – GhostCat Jun 19 '18 at 10:36
  • The errors are many?:- – derrick corfield Jun 19 '18 at 10:36
  • Then rather step back. Create a new class. Put in just a few lines (of which you think: this should work). Then run the compiler. Fix bugs. Add some more lines. Repeat. Dont write 100 lines to then be broken! – GhostCat Jun 19 '18 at 10:37
  • My my. Typically, when you say errors, we assume you are talking about compiler errors. You are talking about an exception at runtime. Which, if you had googled it, has been answered a zillion times. – GhostCat Jun 19 '18 at 10:49
  • So this is a duplicate of: https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it and should be closed as such. And as said: start by reading the messages. It tells you: you are using index 3 on an array that has 0 members. What else would you need to know?! – GhostCat Jun 19 '18 at 10:49
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – GBlodgett Jun 19 '18 at 13:43
  • The questions in GhostCat (5554734) or GBlodgett email did not help. Does somebody know how to added a image to my question, the added image icon does seem to work. – derrick corfield Jun 22 '18 at 12:46

1 Answers1

0

I tried running this snippet of code in my editor:

ArrayList<ArrayList<Integer>> suitsCards = new ArrayList<ArrayList<Integer>>();

for(int i=3;i>-1;i--){
    suitsCards.add(i,new ArrayList<Integer>());
}

(I had to substitute Cards with Integer since I didn't have the complete definition for Cards)

The error I received was as follows:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 0
at java.base/java.util.ArrayList.rangeCheckForAdd(ArrayList.java:682)
at java.base/java.util.ArrayList.add(ArrayList.java:494)
at stack.StackOverflow.main(StackOverflow.java:12)

This is the same error as in the question. The problem is that the for loop starts at 3 and goes down from there. The ArrayList documentation specifies when ArrayList will throw an error:

Throws: IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

Since the size is 0, and the index is 3, the ArrayList should throw an exception. For loops should always start at 0 (for many reasons, including this one). I edited the for loop to:

ArrayList<ArrayList<Integer>> suitsCards = new ArrayList<ArrayList<Integer>>();

for(int i=0;i<4;i++){
    suitsCards.add(i,new ArrayList<Integer>());
}

This loop worked just fine. Also, the second for loop has the same problem (although it may not give an error), which can be fixed like this:

for(int i=0;i<4;i++){
    for(int k=0;k<combinedCardsList.size();k++){
        if(combinedCardsList.get(k).suitValue()==i){
            suitsCards.get(i).add(combinedCardsList.get(k));
        }
    }
} 

There may be other errors, but since only a small amount of code is provided, these are all the ones I can find.

jcroskery
  • 135
  • 2
  • 10
  • Did you get a new error, or was it the same one? And why are the for loops set up that way anyways? – jcroskery Jun 20 '18 at 20:09
  • Only the first 2 for loops causes the problem, the last for loop is only for me to see if arraylist of arraylist is working, which clearly doesn't. The middle nest of for loops fills the 4 arraylist, one for Diamonds, Clubs, Hearts and Spades. Each element of the ArrayList is Card object from combinedCardsList which is arrayList previously set up in the program. A Card object is (int cardNo, Rank rank, Suit suit, BufferedImage tempcardImage, boolean isFaceup, boolean isCompCard). – derrick corfield Jun 21 '18 at 18:57
  • I was referring to the complete card class (to run in my editor). I was also wondering why the first two for loops begin at 3 (i = 3;) instead of 0 (i = 0;), to further clarify. Beginning for loops with non-zero values except for a very good reason is a recipe for disaster. – jcroskery Jun 21 '18 at 19:11