1

I am trying to write a basic java program which collects data from an excel spreadsheet and stores it in an array. The problem I'm having is that I get the ArrayOutOfBounds exception, but can't see where I go outside the bounds of my array. I've even deliberately dimensioned the array to be significantly larger than the loop termination values:

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;





public class InputArray {

    public static void main(String[] args) {
        String[][] InputArray1;
        int i = 0;
        int j = 0;

        InputArray1 = new String[220][220];

        try{
        Workbook OLE1Shots = Workbook.getWorkbook(new File("C:/Book12.xls"));
        Sheet inList = OLE1Shots.getSheet(0);
        for (i = 0; i < 190; i++){
            Cell aCell = inList.getCell(i, 0);
            Cell bCell = inList.getCell(i, 1);
            String strIn = aCell.getContents();
            String strOrd = bCell.getContents();
            if (strIn != ""){
                InputArray1[0][j] = strIn;
                InputArray1[1][j] = strOrd;
                j = j + 1;
            }

        }
        }

        catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (i = 0; i < 190; i++){
            System.out.println(InputArray1[0][i]);
            System.out.println(InputArray1[1][i]);
        }
    }


}

The full message i get is:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2

    at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)

    at InputArray.main(InputArray.java:29)

Would be great if someone could work out what's going wrong here. Hopefully it will turn out to be just me being stupid.

Edit:

I was doing something stupid. What was throwing the error was the fact that I used VBA notation for cell position (row, column), where jxl uses (column, row). I changed these lines:

for (i = 0; i < 190; i++){
    Cell aCell = inList.getCell(i, 0);
    Cell bCell = inList.getCell(i, 1);

to these:

c = inList.getRows();
    for (i = 0; i < c; i++){

         Cell aCell = inList.getCell(0, i);
         Cell bCell = inList.getCell(1, i);

And the code now runs and correctly prints out the strings. However, I do keep getting the message

InputArray as localhost contains obsolete methods. The virtual machine was unable to remove all stack frames running old code from the call stack...
3
  • 1
    I think debugging your code will help... Commented Jan 19, 2014 at 12:02
  • 2
    Before the "for" loop check the size of the lists. Don't try to go beyond that. Commented Jan 19, 2014 at 12:05
  • the on sheet range is 24 x 2 cells with data, so I tried reducing the loop limits to i < 10 as a test, but I am still getting the error. When I debug, as The Lost Mind suggests, I get a message "The source attachment does not contain the source for the file SheetImpl.class". I have tried re-downloading jxl, but I am still getting this message. Commented Jan 19, 2014 at 14:33

1 Answer 1

1

Your variable 'i' goes from 0 to 189, and is fed to the method that reads the cells from the spreadsheet. Does the spreadsheet have that many rows? Be aware that the fact that rows appear in the spreadsheet program does not necessarily mean that the rows are represented in the spreadsheet file.

The message tells you exactly, if not completely, what is wrong -- the index that is too large is '2', the method that reports the error is getCell(), and the line numbers tell you where in your program it is happening.

Sign up to request clarification or add additional context in comments.

3 Comments

Be sure to 'accept' answers that answer your question (and upvoting is nice also); it is part of how stackoverflow works...
Hi rcook. The upvote is mine because I found the hint you gave me useful. I do always try to tick posts that answer the question posed in full. I would classify your post as a useful hint or clue, rather than a full answer, however, which is why I haven't ticked it, but I am grateful for the assistance and it did help me find the answer.
It was something other than overrunning the number of rows?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.