Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Vinx

Thread: Please Help - java.lang.ArrayIndexOutOfBoundsException

  1. #1
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Please Help - java.lang.ArrayIndexOutOfBoundsException

    I'm doing a project from my Book, and I must have accidentally typed something in wrong. I'm not going to post the entire thing because it's 172 lines of code, which I don't believe would be that friendly for a forum board. I'll post the part it's having problems with. I'm horribly confused because I've tried reading over that area of the book about 4 times and it was a exact match. Maybe I'm just blind.

    The IDE I'm using is Eclipse.


    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
    at BeatBox.BuildGui(BeatBox.java:53)
    at BeatBox.main(BeatBox.java:23)
    Java Code:
    public static void main (String[] args) {
        	new BeatBox().BuildGui();
        }
        
        public void BuildGui() {
        	theFrame = new JFrame("Cyber Beatbox");
        	theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	BorderLayout layout = new BorderLayout();
        	JPanel background = new JPanel(layout);
        	background.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        	
        	checkboxList = new ArrayList<JCheckBox>();
        	Box buttonBox = new Box(BoxLayout.Y_AXIS);
        	
        	JButton start = new JButton("Start");
        	start.addActionListener(new MyStartListener());
        	buttonBox.add(start);
        	
        	JButton stop = new JButton("Start");
        	stop.addActionListener(new MyStopListener());
        	buttonBox.add(stop);
        	
        	JButton upTempo = new JButton("Tempo Up");
        	upTempo.addActionListener(new MyUpTempoListener());
        	
        	JButton downTempo = new JButton("Tempo Down");
        	downTempo.addActionListener(new MyDownTempoListener());
        	buttonBox.add(downTempo);
        	
        	Box nameBox = new Box(BoxLayout.Y_AXIS);
        	for (int i = 0; i < 16; i++) {
        		nameBox.add(new Label(instrumentNames[i]));
        	} // end loop
        	
        	background.add(BorderLayout.EAST, buttonBox);
        	background.add(BorderLayout.WEST, nameBox);
        	
        	theFrame.getContentPane().add(background);
        	
        	GridLayout grid = new GridLayout(16,16);
        	grid.setVgap(1);
        	grid.setHgap(2);
        	mainPanel = new JPanel(grid);
        	background.add(BorderLayout.CENTER, mainPanel);
        	
        	for (int i = 0; i < 256; i++) {
        		JCheckBox c = new JCheckBox();
        		c.setSelected(false);
        		checkboxList.add(c);
        		mainPanel.add(c);
        	} // end loop
    
            setUpMidi();
    
            theFrame.setBounds(50,50,300,300);
            theFrame.pack();
            theFrame.setVisible(true);
        }
    Line 23
    Java Code:
        	new BeatBox().BuildGui();
    Line 53
    Java Code:
        		nameBox.add(new Label(instrumentNames[i]));

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default Re: Please Help - java.lang.ArrayIndexOutOfBoundsException

    If you want help, you'll have to provide an SSCCE that demonstrates the problem - not your whole project, but we need to see all of the pertinent lines. We don't need to see any GUI code, but we do need to see where you're declaring the array.

    What is the array? How many indexes does it have? What index are you trying to access?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,431
    Blog Entries
    7
    Rep Power
    20

    Default Re: Please Help - java.lang.ArrayIndexOutOfBoundsException

    What is the size of array 'instrumentNames'?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Re: Please Help - java.lang.ArrayIndexOutOfBoundsException

    Quote Originally Posted by KevinWorkman View Post
    If you want help, you'll have to provide an SSCCE that demonstrates the problem - not your whole project, but we need to see all of the pertinent lines. We don't need to see any GUI code, but we do need to see where you're declaring the array.

    What is the array? How many indexes does it have? What index are you trying to access?
    I'm sorry for my poor formatting, I never knew what a SSCCE is until now. I don't have much experience on programming forum boards.


    There are two arrays.

    Java Code:
    	String[] instrumentNames = {"Bass Drum", "Closed Hi-Hat", "Open Hi-Hat", "Acoustic Snare", "Crash Cymbal", "Hand Clap", "High Tom", "Hi Bongo", "Maracas", "Whistle", "Low Conga","Cowbell","Vibraslap", "Low-mid Tom", "High Agogo"};
        int[] instruments = {35,42,46,38,49,39,50,60,70,72,64,56,58,47,67,63};
    For some reason my code was too long, so it cut my line of code off.

    35 would represent the Bass Drum, 42 would represent the closed hi-hat, etc.

    Java Code:
    ArrayList<JCheckBox> checkboxList;
    Because the checkboxes are on a 16 by 16 grid, it would take 256 check boxes. That's why it's on a loop to generate the checkboxes.


    I'm working on making a SSCCE at the moment, I've never done one of these before. Might take me a bit.


    Quote Originally Posted by JosAH View Post
    What is the size of array 'instrumentNames'?

    kind regards,

    Jos
    16.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Please Help - java.lang.ArrayIndexOutOfBoundsException

    Quote Originally Posted by Vinx View Post
    ....
    16.
    The JVM is saying otherwise. It's throwing the AIOOBE for the index 15:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15

    Regardless, don't hard-code your for loops like that. Instead let the array itself tell the for loop how many times to loop.

    So instead of hard-coding the for loop end index like you did:

    Java Code:
            Box nameBox = new Box(BoxLayout.Y_AXIS);
            for (int i = 0; i < 16; i++) {
                nameBox.add(new Label(instrumentNames[i]));
            } // end loop
    Again, let the array dictate it:

    Java Code:
            Box nameBox = new Box(BoxLayout.Y_AXIS);
            for (int i = 0; i < instrumentNames.length; i++) {
                nameBox.add(new Label(instrumentNames[i]));
            } // end loop

  6. #6
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Re: Please Help - java.lang.ArrayIndexOutOfBoundsException

    Quote Originally Posted by Fubarable View Post
    The JVM is saying otherwise. It's throwing the AIOOBE for the index 15:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15

    Regardless, don't hard-code your for loops like that. Instead let the array itself tell the for loop how many times to loop.

    So instead of hard-coding the for loop end index like you did:

    Java Code:
            Box nameBox = new Box(BoxLayout.Y_AXIS);
            for (int i = 0; i < 16; i++) {
                nameBox.add(new Label(instrumentNames[i]));
            } // end loop
    Again, let the array dictate it:

    Java Code:
            Box nameBox = new Box(BoxLayout.Y_AXIS);
            for (int i = 0; i < instrumentNames.length; i++) {
                nameBox.add(new Label(instrumentNames[i]));
            } // end loop
    Thank you so much Fubarable. That's a great idea, wish I would have thought of that instead of following directly what the book says. Maybe it was a editing error.

    It's working perfectly fine now, thanks again to everyone who contributed.

    Please Help - java.lang.ArrayIndexOutOfBoundsException-kcly5.png


    Never knew that you could learn so much from a little problem.
    Fubarable likes this.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Please Help - java.lang.ArrayIndexOutOfBoundsException

    Great. Glad you've got things working now!

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException: 20
    By xminiguyx in forum New To Java
    Replies: 1
    Last Post: 12-14-2011, 04:54 PM
  2. java.lang.ArrayIndexOutOfBoundsException
    By rajasohaibmaroof in forum AWT / Swing
    Replies: 15
    Last Post: 09-29-2011, 07:46 PM
  3. java.lang.ArrayIndexOutOfBoundsException
    By riccian in forum New To Java
    Replies: 0
    Last Post: 03-18-2008, 09:38 AM
  4. java.lang.ArrayIndexOutOfBoundsException
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-02-2007, 09:40 PM
  5. java.lang.ArrayIndexOutOfBoundsException
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 05:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •