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.
Quote:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 15
at BeatBox.BuildGui(BeatBox.java:53)
at BeatBox.main(BeatBox.java:23)
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
Code:
new BeatBox().BuildGui();
Line 53
Code:
nameBox.add(new Label(instrumentNames[i]));
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?
Re: Please Help - java.lang.ArrayIndexOutOfBoundsException
What is the size of array 'instrumentNames'?
kind regards,
Jos
Re: Please Help - java.lang.ArrayIndexOutOfBoundsException
Quote:
Originally Posted by
KevinWorkman
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.
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.
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
What is the size of array 'instrumentNames'?
kind regards,
Jos
16.
Re: Please Help - java.lang.ArrayIndexOutOfBoundsException
Quote:
Originally Posted by
Vinx
....
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:
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:
Code:
Box nameBox = new Box(BoxLayout.Y_AXIS);
for (int i = 0; i < instrumentNames.length; i++) {
nameBox.add(new Label(instrumentNames[i]));
} // end loop
1 Attachment(s)
Re: Please Help - java.lang.ArrayIndexOutOfBoundsException
Quote:
Originally Posted by
Fubarable
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:
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:
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.
Attachment 3862
Never knew that you could learn so much from a little problem.
Re: Please Help - java.lang.ArrayIndexOutOfBoundsException
Great. Glad you've got things working now!