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

StackOverflow https://stackoverflow.com/questions/18273422

  •  24-06-2022
  •  | 
  •  

Frage

import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class SpreadsheetRead {
    /**
     * @param args
     * @throws IOException 
     * @throws BiffException 
     */
    public static void main(String[] args) throws BiffException, IOException {
        // TODO Auto-generated method stub
        Workbook workbook = Workbook.getWorkbook(new File("Book1.xls"));
        Sheet sheet = workbook.getSheet(0);

        Cell name = sheet.getCell(0, 0);
        Cell name1 = sheet.getCell(1, 0);
        try {
            Cell name2 = sheet.getCell(2, 0);
            Cell name3 = sheet.getCell(3, 0);
        }catch (Exception e){
            e.printStackTrace();
        }

        Cell value = sheet.getCell(0, 1);
        Cell value1 = sheet.getCell(1, 1);
        Cell value2 = sheet.getCell(2, 1);
        Cell value3 = sheet.getCell(3, 1);

        System.out.println(sheet.getRows());
        System.out.println(sheet.getColumns());
        System.out.println(name.getContents());
        System.out.println(name1.getContents());
        System.out.println(value.getContents());
        System.out.println(value1.getContents());
    }
}

Excel Sheet Content (Book1.xls) ... It Contains 4 Rows and 2 Columns as mentioned below.

Name Value
A    1
B    2
C    3

This code is working for cell [(0,0),(0,1),(1,0) & (1,1)] and for rest of the cells it is giving ArrayIndexOutOfBoundException... please help

Stack Trace

java.lang.ArrayIndexOutOfBoundsException: 2
4
2
Name
Value
A
    at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)
    at SpreadsheetRead.main(SpreadsheetRead.java:25)
1
War es hilfreich?

Lösung

The console output is slightly tangled up; data logged to System.err overlap those logged to System.out:

java.lang.ArrayIndexOutOfBoundsException: 2
4
2
Name
Value
A
    at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)
    at SpreadsheetRead.main(SpreadsheetRead.java:25)
1

Let's untangle those, and match them with the respective logging calls:

Error log:

java.lang.ArrayIndexOutOfBoundsException: 2
    at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356)
    at SpreadsheetRead.main(SpreadsheetRead.java:25)

Line 25 is where I am trying to access Cell (2,0)

this indicates that Cell(2,0) is not in the table.

Let's look at the System.out log.

they are also failed it just that I commented those lines when I executed this code.

I assume you mean the access to column 1 and the respective logging calls.

Cell name = sheet.getCell(0, 0);
Cell name1 = sheet.getCell(1, 0);
Cell value = sheet.getCell(0, 1);
Cell value1 = sheet.getCell(1, 1);

System.out.println(sheet.getRows());      //4
System.out.println(sheet.getColumns());   //2
System.out.println(name.getContents());   //Name
System.out.println(name1.getContents());  //Value
System.out.println(value.getContents());  //A
System.out.println(value1.getContents()); //1

Comparing this to the table:

Name Value
A    1
B    2
C    3

indicates that the first argument is the column index, not row index as you seem to expect.

the Sheet#getCell documentation confirms this:

public Cell getCell(int column,
                    int row)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top