Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Array Out Of Index Exception, Not Sure Why It's Occurring.

  1. #1
    Junior Member
    Join Date
    May 2014
    Location
    Inverness, United Kingdom
    Posts
    11
    My Mood
    Cheerful
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question Array Out Of Index Exception, Not Sure Why It's Occurring.

    My code (below) occasionally decides to return this error:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at com.github.bewd.yugioh.main.Main.main(Main.java:94 )

    Can anyone help me?

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    //import javax.swing.JLabel;
    import java.util.ArrayList;
    import java.util.Random;
    import java.util.Scanner;
    import java.math.*;
     
    @SuppressWarnings("serial")
    public class Main extends JPanel{
     
        private static final int appversionmajor = 0;
        private static final double appversionminor = 1.3;
        private static final String appname = "Yu-Gi-Oh!: Trading Card Game";
        private static final String appstage = "Alpha";
        private static final String versionstring = appname + " (" + appversionmajor + "." + appversionminor + " " + appstage + ")";
        private BufferedImage sdk1, sdk2, sdk3, sdk4, sdk5, sdk6, sdk7, sdk8, sdk9, sdk10, bc;
     
        public Main() {
     
            try {                
                sdk1 = ImageIO.read(getClass().getResourceAsStream("img\\cards\\sdk001.png"));
                sdk2 = ImageIO.read(getClass().getResourceAsStream("img\\cards\\sdk002.png"));
                sdk3 = ImageIO.read(getClass().getResourceAsStream("img\\cards\\sdk003.png"));
                sdk4 = ImageIO.read(getClass().getResourceAsStream("img\\cards\\sdk004.png"));
                sdk5 = ImageIO.read(getClass().getResourceAsStream("img\\cards\\sdk005.png"));
            } 
            catch (IOException e) {
                Logger LogErr = Logger.getLogger(Main.class.getName());
                System.err.println(appname + " " + "Caught IOException: " + e.getMessage());
           }
        }
     
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(sdk1, 0, 0, Card.cardwidth / 5, Card.cardheight / 5, null);
            g.drawImage(sdk2, Card.cardlong, 0, Card.cardwidth / 5, Card.cardheight / 5, null);
            g.drawImage(sdk3, Card.cardlong * 2, 0, Card.cardwidth / 5, Card.cardheight / 5, null);
            g.drawImage(sdk4, Card.cardlong * 3, 0, Card.cardwidth / 5, Card.cardheight / 5, null);
            g.drawImage(sdk5, Card.cardlong * 4, 0, Card.cardwidth / 5, Card.cardheight / 5, null);
        }
     
    	public static void main(String[] args) {
                JFrame frame = new JFrame(versionstring);
                frame.add(new Main());
                frame.setSize(1020, 680);
                frame.setVisible(false);
                frame.setResizable(false);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
     
                ArrayList<MonsterCard> deck1 = new ArrayList(50);
                ArrayList<MonsterCard> deck2 = new ArrayList(50);
                deck1.add(CardListKaiba.BlueEyesWhiteDragon);
                deck1.add(CardListKaiba.HitotsuMeGiant);
                deck1.add(CardListKaiba.RyuKishin);
                deck1.add(CardListKaiba.TheWickedWormBeast);
                deck1.add(CardListKaiba.BattleOx);
                /*deck1.add(CardListKaiba.KoumoriDragon);
                deck1.add(CardListKaiba.JudgeMan);
                deck1.add(CardListKaiba.RogueDoll);
                deck1.add(CardListKaiba.Kojikocy);
                deck1.add(CardListKaiba.Uraby); */
                ArrayList<MonsterCard> monsterfield1 = new ArrayList(5);
                ArrayList<MonsterCard> monsterfield2 = new ArrayList(5);
                ArrayList<SpellCard> magicfield1 = new ArrayList(5);
                ArrayList<SpellCard> magicfield2 = new ArrayList(5);
                ArrayList<MonsterCard> hand1 = new ArrayList(5);
                ArrayList<MonsterCard> hand2 = new ArrayList(5);
                int lifepoints1 = 2000;
                int lifepoints2 = 2000;
                Random r = new Random();
     
                System.out.println("Drawing Hand from Deck...");
                int randomPos1 = r.nextInt(deck1.size());
                    hand1.add(deck1.get(randomPos1));
                    deck1.remove(randomPos1);
                    hand1.add(deck1.get(randomPos1));
                    deck1.remove(randomPos1);
                    hand1.add(deck1.get(randomPos1));
                    deck1.remove(randomPos1);
                    hand1.add(deck1.get(randomPos1));
                    deck1.remove(randomPos1);
     
                while(lifepoints1 > 0|| lifepoints2 > 0){
                    hand1.add(deck1.get(randomPos1));
                    deck1.remove(randomPos1);
                    System.out.println("Initiating Draw Phase:");
                    System.out.println("Your hand consists of:");
                    for(int i = 0; i <hand1.size(); i++){
                        System.out.println(hand1.get(i).toString() + "");
                    }
     
     
                System.out.println("Initiating Main Phase 1:");
                System.out.println("Which monster would you like to summon?");
                Scanner monSummon = new Scanner(System.in);
                int whichMonsterPOS1 = monSummon.nextInt();
                monsterfield1.add(hand1.get(whichMonsterPOS1));
                hand1.remove(whichMonsterPOS1);
                for(int i = 0; i<monsterfield1.size(); i++){
                    System.out.println(monsterfield1.get(i).toString() + " has been summoned");
                }   
     
                frame.setVisible(false);
                System.out.println("Your side of the Field contains: " + monsterfield1);
                break;
            }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array Out Of Index Exception, Not Sure Why It's Occurring.

    The max index to an array is the length-1. Indexes range from 0 to the length-1
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Location
    Inverness, United Kingdom
    Posts
    11
    My Mood
    Cheerful
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Array Out Of Index Exception, Not Sure Why It's Occurring.

    Quote Originally Posted by Norm View Post
    The max index to an array is the length-1. Indexes range from 0 to the length-1
    How would I go about fixing this?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Array Out Of Index Exception, Not Sure Why It's Occurring.

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at com.github.bewd.yugioh.main.Main.main(Main.java:94 )
    Make sure the code on line 94 does not use an index that is past the end of the list.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Location
    Inverness, United Kingdom
    Posts
    11
    My Mood
    Cheerful
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Array Out Of Index Exception, Not Sure Why It's Occurring.

    Sorted it now by declaring an int with a for loop further up; thanks.

Similar Threads

  1. Airline having with array index out of bounds exception
    By dubs4sam in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 22nd, 2014, 02:57 PM
  2. Help needed for string index out of bounds exception
    By BFF in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 11th, 2013, 10:48 PM
  3. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM
  4. [SOLVED] Array Index out of bounds Exception
    By Tohtekcop in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 19th, 2012, 03:03 PM
  5. ArrayList initial capacity problem (Index out of bounds Exception)
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 11:24 AM

Tags for this Thread