ArrayIndexOutOfBounds error
Hi all, when running the following code, I get an ArrayIndexOutOfBounds: 255 error. I should note that getHist() returns an int[3][255]. I'm sure there's something simple that I should be doing, but I don't know what it is. Any ideas?
privatevoid saveFile(){
BufferedImage bi = leftPanel.img;
int[][] image_data = getHist(bi);
//new int[3][256];
//image_data = getHist(bi);
//System.out.println(image_data.length);
JFileChooser saveChoose =new JFileChooser();
int result = saveChoose.showSaveDialog(this);
if (result == JFileChooser.CANCEL_OPTION)return;
if(result == JFileChooser.APPROVE_OPTION)
try{
FileWriter fw =new FileWriter(saveChoose.getSelectedFile());
BufferedWriter bw =new BufferedWriter(fw);
for(int i = 0; i < image_data.length; i++){
for(int j = 0; j < image_data[i].length; j++){
//System.out.println(image_data[i].length);
if(j == (image_data[i].length - 1)){
//System.out.println("Condition ok " + image_data[i].length );
bw.write(String.valueOf(image_data[j][i]) );
System.out.println("Data ok");
bw.newLine();
}
else{
bw.write( String.valueOf(image_data[i]) +"\t");
}
}
}
bw.close();
}catch(Exception e){
System.out.println("Error: " + e);
}
}
Thanks,
Joe
[2626 byte] By [
jmarottaa] at [2007-11-26 16:59:40]

Duh. That's a pretty dumb mistake. Sorry about that.
Now, interestingly, even though I *should* only have 3 columns (at least, so I think...), I have 12! (that's 12, not 12*11*10...)
I'm sure it's got to do with how I'm printing this out. Ideally, I'd like this to be a tab-delimited text file, with 3 columns and 256 rows. Any further (incredibly) helpful suggestions?
Thanks!
Well, an analagous version. And maybe that's it?
I tried:
for(int i = 0; i < image_data.length; i++){
for(int j = 0; j<image_data[i].length; j++){
bw.write(String.valueOf(image_data[i][j]) );
bw.write("\t");
}
bw.newLine();
//bw.newLine();
}
But it just gave the same 12 columns. Since I'm trying to write to file, it may be some sort of formatting thing? I'm not sure. Thank you for the help, though.>
I generalized my example so it can write to System.out or a BufferedWriter:
import java.io.*;
public class Example {
static void print(Appendable out, int[][] m) throws IOException {
String lineSeparator = System.getProperty("line.separator");
for(int i=0; i<m.length; ++i) {
for (int j=0; j >< m[i].length; j++) {
out.append(String.valueOf(m[i][j])).append("\t");
}
out.append(lineSeparator);
}
}
public static void main(String[] args) throws IOException {
int[][] data = {
{0,2,4,6,8},
{1,3,5,7,9},
};
print(System.out, data);
BufferedWriter bw = new BufferedWriter(new FileWriter("junk.txt"));
try {
print(bw, data);
} finally {
bw.close();
}
}
}
I wound up going with something a little "riskier":
for(int i = 0; i < image_data[0].length; i++){
for(int j = 0; j < image_data.length; j++){
bw.write(String.valueOf(image_data[j][i]) );
if(j < (image_data.length -1)){
bw.write("\t");
}
}
bw.newLine();
}
bw.close();
I was having trouble with the appendable stuff, and, honestly, didn't want to pass any variables to this method. Thank you for all your help.