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]
# 1
arrays are indexed from 0, not 1. so the last element of an array is at position array.length -1, rather than array.length as you've assumed
georgemca at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...
# 2
shouln't thisbw.write(String.valueOf(image_data[j][i]) );be thisbw.write(String.valueOf(image_data[i][j]) );~Tim
SomeoneElsea at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...
# 3
A wild guess:String.valueOf(image_data[j][i]) should have beenString.valueOf(image_data[i][j])
DrLaszloJamfa at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...
# 4
Punchbug!
DrLaszloJamfa at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...
# 5
> Punchbug!We'll have none of that language around here!!:)Tim
SomeoneElsea at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...
# 6

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!

jmarottaa at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...
# 7

public class Example {

static void print(int[][] m) {

for(int i=0; i<m.length; ++i) {

for (int j=0; j><m[i].length; j++) {

System.out.print(m[i][j]);

System.out.print("\t");

}

System.out.println();

}

}

public static void main(String[] args) {

int[][] data = {

{0,2,4,6,8},

{1,3,5,7,9},

};

print(data);

}

}

>

DrLaszloJamfa at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...
# 8
Formatting like this yields the same issue. I understand what you're getting at, but I'm unclear as to what's going wrong...
jmarottaa at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...
# 9
Are you saying your data is int[*][3] but your code is printing it out as though there were 12 columns, or are you saying that youe data is int[*][12] and you'd like to only print 3 ints per row (ie, break every matrix row into 4 printed rows)?
DrLaszloJamfa at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...
# 10
I'm saying the data is int[3][256] and it's printing in 12 columns.
jmarottaa at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...
# 11
> I'm saying the data is int[3][256] and it's printing in 12 columns.Even when you use the print method of reply #7?
DrLaszloJamfa at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...
# 12

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.>

jmarottaa at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...
# 13

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();

}

}

}

DrLaszloJamfa at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...
# 14

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.

jmarottaa at 2007-7-8 23:27:27 > top of Java-index,Java Essentials,New To Java...