I'm using arraylists in java to store and process data from a file. On moderate or small data sets it works perfectly fine, but exceptionally large data sets throw an ArrayIndexOutOfBoundsException with no specification on which line of code threw the exception. Could this be a bug or a problem with running out of memory? The code doesn't hard code a data size, so there's no way that an index that doesn't exist is being called.
Edit: This is the method throwing the error:
public static ArrayList<Grain> FindGrains(Point[][] p){
ArrayList<Grain> grains = new ArrayList<Grain>();
ArrayList<Point> growthPoints = new ArrayList<Point>();
ArrayList<Point> previousPoints = new ArrayList<Point>();
boolean finGrainGrow = false;
int x = -1;
int y = -1;
int j, k;
int grainNum =-1;
double stepSize = 0.5;
while(true){
x = -1;
y = -1;
growthPoints.clear();
previousPoints.clear();
for(int f =0; f<p.length;f++){
for(int r =0; r<p[0].length;r++){
if(p[r][f].getGrainNum() == -1){
x = r;
y = f;
grains.add(new Grain());
grainNum++;
p[x][y].setGrainNum(grainNum);
grains.get(grainNum).add(p[x][y]);
growthPoints.add(0,p[x][y]);
break;
}
}
if (x!=-1) break;
}
if (x==-1) break;
finGrainGrow = false;
while(!finGrainGrow){
finGrainGrow = true;
previousPoints.clear();
previousPoints.ensureCapacity(growthPoints.size());
for(int q=0;q<growthPoints.size();q++){
previousPoints.add(q,growthPoints.get(q));
}
growthPoints = new ArrayList<Point>();
for(int h = 0; h<previousPoints.size(); h++){
j = (int)(previousPoints.get(h).getX()/stepSize);
k = (int)(previousPoints.get(h).getY()/stepSize);
try{
if(checkMisorientation(p[j][k],p[j-1][k]) && p[j-1][k].getGrainNum() ==-1){
grains.get(grainNum).add(p[j-1][k]);
growthPoints.add(p[j-1][k]);
p[j-1][k].setGrainNum(grainNum);
finGrainGrow = false;
}
}
catch(ArrayIndexOutOfBoundsException t){
}
try{
if(checkMisorientation(p[j][k],p[j+1][k]) && p[j+1][k].getGrainNum() ==-1){
grains.get(grainNum).add(p[j+1][k]);
growthPoints.add(p[j+1][k]);
p[j+1][k].setGrainNum(grainNum);
finGrainGrow = false;
}
}
catch(ArrayIndexOutOfBoundsException t){
}
try{
if(checkMisorientation(p[j][k],p[j][k-1]) && p[j][k-1].getGrainNum() ==-1){
grains.get(grainNum).add(p[j][k-1]);
growthPoints.add(p[j][k-1]);
p[j][k-1].setGrainNum(grainNum);
finGrainGrow = false;
}
}
catch(ArrayIndexOutOfBoundsException t){
}
try{
if(checkMisorientation(p[j][k],p[j][k+1]) && p[j][k+1].getGrainNum() ==-1){
grains.get(grainNum).add(p[j][k+1]);
growthPoints.add(p[j][k+1]);
p[j][k+1].setGrainNum(grainNum);
finGrainGrow = false;
}
}
catch(ArrayIndexOutOfBoundsException t){
}
}
}
}
return grains;
}
I have objects called Point, which contain 5 double values and an int(an x and y coordinate, three euler angles, and an int of which grain it belongs to). A two dimensional array of points is initialized with a grain number of -1 and generic values for the other parameters. The correct values are then read in from a file and saved to the appropriate point. I'm trying to sort adjacent Points into objects called Grain, which are just ArrayLists of Points. The error being thrown is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
The main method calls the FindGrains method. It doesnt give a line number for where the exception is encountered so I really have no idea what I'm doing. Any help is very much appreciated.