ArrayIndexOutOfBoundsException problem

I'm having a problem itterating through an array. While itterating through the array, i get an ArrayIndexOutOfBoundsException when I hit the second element (data[1]). The program runs correctly, but the exception keeps coming up. Catching the exception would solve the problem, but i am trying to find solution that would get rid of the exception all together.

[368 byte] By [aserothbwa] at [2007-11-26 20:16:37]
# 1
If data[1] throws the exception, then the length of the array is 1. Try printing out data.length before the loop to verify. Since the one and only element in the array is [0], going past that is a problem.
hunter9000a at 2007-7-10 0:39:27 > top of Java-index,Java Essentials,Java Programming...
# 2

> I'm having a problem itterating through an array.

> While itterating through the array, i get an

> ArrayIndexOutOfBoundsException when I hit the second

> element (data[1]).

Then there is no data[1]. It has only one element, data[0].

> The program runs correctly, but

> the exception keeps coming up. Catching the exception

> would solve the problem,

No, it would hide the problem.

jverda at 2007-7-10 0:39:27 > top of Java-index,Java Essentials,Java Programming...
# 3
>The program runs correctly, but the exception keeps coming up.:rollseyes:
DrLaszloJamfa at 2007-7-10 0:39:27 > top of Java-index,Java Essentials,Java Programming...
# 4

but there is a data[1] in the array. the array itself has 6 elements. For example, in my code, i did a manual iteration through all the elements of data:

System.out.println(data[0]);

System.out.println(data[1]);

System.out.println(data[2]);

System.out.println(data[3]);

System.out.println(data[4]);

System.out.println(data[5]);

This will not only comply, but it will also display all the data then throw the exception.

aserothbwa at 2007-7-10 0:39:27 > top of Java-index,Java Essentials,Java Programming...
# 5
Post a tiny (<1 page) example program that can demonstrate your problem.It's hard to help you if you can't do this.
DrLaszloJamfa at 2007-7-10 0:39:27 > top of Java-index,Java Essentials,Java Programming...
# 6

> but there is a data[1] in the array. the array itself

> has 6 elements. For example, in my code, i did a

> manual iteration through all the elements of data:

>

> System.out.println(data[0]);

> System.out.println(data[1]);

> System.out.println(data[2]);

> System.out.println(data[3]);

> System.out.println(data[4]);

> System.out.println(data[5]);

>

> This will not only comply, but it will also display

> all the data then throw the exception.

Then accessing data[1] will not throw that exception.

Post code.

When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

jverda at 2007-7-10 0:39:27 > top of Java-index,Java Essentials,Java Programming...
# 7

This is the method that is throwing the exception

private void setData( String dataToParse[] ) {

//test to make sure data chunks are coming in

for (String n : dataToParse) {

System.out.println(n);

//parses the data

String data[] = n.split("[\\*]");

//checks each element of the data array exception occurs after the

//6th println

System.out.println(parseData[0]);

System.out.println(parseData[1]);

System.out.println(parseData[2]);

System.out.println(parseData[3]);

System.out.println(parseData[4]);

System.out.println(parseData[5]);

/*additional coding that never gets executed */

}

}

the data itself is essentually lines of data broken into 6 groupings by a * (ie. a*b*sd*54*f3*d12) . Going into the method is an array of strings that contain the data read in from the data file 1 line per array element Ideally, the method takes each line, parses it, then stores the data in various class variables, doing some calculation when necessary. However, as it stands, the program does not go past the last println.

aserothbwa at 2007-7-10 0:39:28 > top of Java-index,Java Essentials,Java Programming...
# 8

public class ArrayExample {

private void setData( String dataToParse[] ) {

assert(dataToParse != null);

for (String n : dataToParse) {

assert(n != null);

System.out.println(n);

//parses the data

String data[] = n.split("[\\*]");

assert(data.length >= 6);

//checks each element of the data array exception occurs after the

//6th println

System.out.println(dataToParse[0]);

System.out.println(dataToParse[1]);

System.out.println(dataToParse[2]);

System.out.println(dataToParse[3]);

System.out.println(dataToParse[4]);

System.out.println(dataToParse[5]);

/*additional coding that never gets executed */

}

}

}

What would be more helpful is a sort *complete* program that we could run.

DrLaszloJamfa at 2007-7-10 0:39:28 > top of Java-index,Java Essentials,Java Programming...
# 9

>

> private void setData( String dataToParse[] ) {

>//test to make sure data chunks are coming in

> for (String n : dataToParse) {

>System.out.println(n);

>//parses the data

>String data[] = n.split("[\\*]");

> //checks each element of the data array

> exception occurs after the

>//6th println

> System.out.println(parseData[0]);

>System.out.println(parseData[1]);

> System.out.println(parseData[2]);

>System.out.println(parseData[3]);

> System.out.println(parseData[4]);

>System.out.println(parseData[5]);

>

>/*additional coding that never gets executed */

>

> }

>

Umm... where is this parseData array coming from? I see dataToParse as an input param, and a String[] data, but no where do I see parseData being defined or populated.

MrPicklesa at 2007-7-10 0:39:28 > top of Java-index,Java Essentials,Java Programming...
# 10

> This is the method that is throwing the exception

>

> private void setData( String dataToParse[] ) {

>//test to make sure data chunks are coming in

> for (String n : dataToParse) {

>System.out.println(n);

>//parses the data

>String data[] = n.split("[\\*]");

> //checks each element of the data array

> exception occurs after the

>//6th println

> System.out.println(parseData[0]);

>System.out.println(parseData[1]);

> System.out.println(parseData[2]);

>System.out.println(parseData[3]);

> System.out.println(parseData[4]);

>System.out.println(parseData[5]);

>

>/*additional coding that never gets executed */

>

> }

So, you're not showing the line of code that throws the exception? Or is it parseData[5] that throws it?

Jeez, you've got dataToParse, data, and parseData. Using more meaningful variable names might help you to not get confused and not make whatever mistake you're making here.

jverda at 2007-7-10 0:39:28 > top of Java-index,Java Essentials,Java Programming...
# 11

Because of the size and nature of the program, i cannot post it. Here is a cut down version of the file reading process that takes the data, then calls the previous method in the dataPage class.

public void main (String args[]) {

ArrayList<dataPage> systemPages = new ArrayList<dataPage>();

String data;

try {

File rFile = new File("SystemTypeData.txt");

FileReader readerO = new FileReader(rFile);

BufferedReader buffRead = new BufferedReader(readerO);

while((data = buffRead.readLine()) != null) {

//reads in the first line, and parses it

String dataParse[] = data.split("[\\*]");

//the last piece of the line is the number of sub lines of data that

//are associated with the main data

int numLine = Integer.parseInt(dataParse[4]);

//creates a new array based on the number of lines to be read in

String dataToParse[] = new String[numLine];

//reads in the number lines of sub data

for (int x = 0; x != numLine; x++) {

dataToParse[x] = buffRead.readLine();

}

//calls the class constructor, which calls the setData

systemPages.add(new dataPage(dataParse[0], dataParse[1],

dataParse[2], dataParse[3], dataToParse ));

}

} catch(IOException ex) {

System.out.println(

"Problen reading from data file, shutting down program.");

System.exit(0);

}

}

The process is in two parts. First, the primary data is read in. From that primary data, the number of lines of sub data is determined, then read into an array of strings. The main data, and the array of sub data, is then used to create a new dataPage that is added to the SystemPages arrayList. In the dataPage class, the main data is placed in private variables, and the array is sent to setData for parsing.

aserothbwa at 2007-7-10 0:39:28 > top of Java-index,Java Essentials,Java Programming...
# 12

I'm not following what you're saying, and I can't tell where the exception is thrown in that code.

For the array in question, right before the line that throws the exception, print out the length of the array, and print out the results of java.util.Arrays.toString(theArray), and print out which index you're trying to access. This will show you where your error is.

jverda at 2007-7-10 0:39:28 > top of Java-index,Java Essentials,Java Programming...
# 13
> Because of the size and nature of the program, i cannot post it. That's why you should post a small *example* program.I can't run your code because it's not complete.
DrLaszloJamfa at 2007-7-10 0:39:28 > top of Java-index,Java Essentials,Java Programming...
# 14

Anyway, just looking at your code...

ArrayList<dataPage> systemPages = new ArrayList<dataPage>();

Naming convention: DataPage -- types start with a capital letter

int numLine = Integer.parseInt(dataParse[4]);

Check length of dataParse array before accessing any elements.

The file could have been misformatted.

for (int x = 0; x != numLine; x++) {

dataToParse[x] = buffRead.readLine();

}

If the file is shorter than you anticipate, readLine() will return null!

Do you check for this?

systemPages.add(new dataPage(dataParse[0], dataParse[1], dataParse[2], dataParse[3], dataToParse ));

Again, this is making an assumption about an array's length.

Why not just pass the array and let the constructor check it's length, etc?

catch(IOException ex) {

System.out.println("...");

At the very least, do a ex.printStackTrace() to see what when wrong and where.

DrLaszloJamfa at 2007-7-10 0:39:28 > top of Java-index,Java Essentials,Java Programming...