質問

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...
役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top