Results 1 to 7 of 7
- 06-19-2012, 06:11 PM #1
Member
- Join Date
- May 2012
- Location
- USA
- Posts
- 38
- Rep Power
- 0
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)Line 23Java 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 53Java Code:new BeatBox().BuildGui();
Java Code:nameBox.add(new Label(instrumentNames[i]));
- 06-19-2012, 06:19 PM #2
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!
- 06-19-2012, 06:25 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Re: Please Help - java.lang.ArrayIndexOutOfBoundsException
What is the size of array 'instrumentNames'?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-19-2012, 06:29 PM #4
Member
- Join Date
- May 2012
- Location
- USA
- Posts
- 38
- Rep Power
- 0
Re: Please Help - java.lang.ArrayIndexOutOfBoundsException
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.
For some reason my code was too long, so it cut my line of code off.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};
35 would represent the Bass Drum, 42 would represent the closed hi-hat, etc.
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.Java Code:ArrayList<JCheckBox> checkboxList;
I'm working on making a SSCCE at the moment, I've never done one of these before. Might take me a bit.
16.
-
Re: Please Help - java.lang.ArrayIndexOutOfBoundsException
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:
Again, let the array dictate it:Java Code:Box nameBox = new Box(BoxLayout.Y_AXIS); for (int i = 0; i < 16; i++) { nameBox.add(new Label(instrumentNames[i])); } // end loop
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
- 06-19-2012, 06:42 PM #6
Member
- Join Date
- May 2012
- Location
- USA
- Posts
- 38
- Rep Power
- 0
Re: Please Help - java.lang.ArrayIndexOutOfBoundsException
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.

Never knew that you could learn so much from a little problem.
-
Similar Threads
-
java.lang.ArrayIndexOutOfBoundsException: 20
By xminiguyx in forum New To JavaReplies: 1Last Post: 12-14-2011, 04:54 PM -
java.lang.ArrayIndexOutOfBoundsException
By rajasohaibmaroof in forum AWT / SwingReplies: 15Last Post: 09-29-2011, 07:46 PM -
java.lang.ArrayIndexOutOfBoundsException
By riccian in forum New To JavaReplies: 0Last Post: 03-18-2008, 09:38 AM -
java.lang.ArrayIndexOutOfBoundsException
By mew in forum New To JavaReplies: 2Last Post: 12-02-2007, 09:40 PM -
java.lang.ArrayIndexOutOfBoundsException
By Marcus in forum New To JavaReplies: 1Last Post: 07-05-2007, 05:15 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks