-3

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.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • Show us the code and tell you are not populating that arraylist from read a file or CSV etc. – Shahzeb Oct 12 '11 at 03:02
  • 4
    I find it a little hard to believe that you're not getting an exception trace that shows you where the exception is occurring. – Hot Licks Oct 12 '11 at 03:31

1 Answers1

1

This is certainly not related to OutOfMemoryException.

Please look at all the places in code if you are calling any of the following APIs of your ArrayList: get(index), set(index, data),add(index, data), remove(index), addAll(index, collection), removeRange(fromIndex, toIndex)

Your focus of debugging should be around the piece of code where you are using any of these APIs of ArrayList. Your code is certainly trying to access an index of your ArrayList instance which does not exist.

Saurabh
  • 7,894
  • 2
  • 23
  • 31
  • Sorry for the lack of info. I've edited the initial question. All calls to get, set, add, and remove are valid calls. I really have no idea where the problem is. It might be an error in a call to a regular two dimensional array I'm using, but I've used embedded for loops to print out the contents of that array and it doesn't throw an error. Also, like I said, the code works for normal size grids of Points, just not large ones. – user990644 Oct 12 '11 at 04:31
  • Could you please check the values of i, j etc before these lines if(checkMisorientation(p[j][k],p[j-1][k]) && p[j-1][k].getGrainNum() ==-1) if(checkMisorientation(p[j][k],p[j+1][k]) && p[j+1][k].getGrainNum() ==-1) and size of p and p[0] etc and see you are not jumping on invalid index – Saurabh Oct 12 '11 at 04:59
  • That's pretty embarassing. p[0].length refers to the y-size of the p array and p.length refers to the x size. I had them mixed up. Nothing to see here folks! Just some stupidity! – user990644 Oct 12 '11 at 05:56