Results 1 to 4 of 4
  1. #1
    CrashWave is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default ArrayIndexOutofBounds Exception on an Excel File

    So I am attempting to write a program that reads in values from the first two columns of an excel spreadsheet and then prints them out in a JSwing application. Unfortunately, even though the example spreadsheet has two columns of 15 values each the following message is generated at the end of the code.

    Error Message:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
    at jxl.read.biff.SheetImpl.getCell(SheetImpl.java:356 )
    at RegAnalysis.populateData(RegAnalysis.java:228)
    at RegAnalysis.access$0(RegAnalysis.java:217)
    at RegAnalysis$1.actionPerformed(RegAnalysis.java:147 )

    Code that generates this:
    private void populateData(Workbook workbook){
    Sheet sheet = workbook.getSheet(0);
    int x = 0;
    int y = 0;
    Cell cell = sheet.getCell("B7");
    System.out.println("Row: " + cell.getRow() + "Cell: " + cell.getColumn());
    do{
    String xferString = sheet.getCell(x,0).getContents();
    double xferDouble = Double.parseDouble(xferString);
    columnA[x] = xferDouble;
    x++;
    }while(sheet.getCell(x,0) != null);
    do{
    String xferString = sheet.getCell(y,1).getContents();
    double xferDouble = Double.parseDouble(xferString);
    columnB[y] = xferDouble;
    y++;
    }while(sheet.getCell(y,1).getContents() != null);
    }

    what do you think could be causing this problem? How do I avoid going out of bounds?

  2. #2
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Your first while loop tries to check the cell for null, but it's bombing in the call to getCell() because the third time through the loop, x is 2, and that's past the bounds of a 2-element array. Even after you fix this, you'll have the same problem in the y loop.

  3. #3
    CrashWave is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    How do you suggest looping through the first two columns of excel? I even added a debug line to print the value of B7 so Im sure there are more values than row 2.

  4. #4
    CrashWave is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default

    Well I'm trying to access a spreadsheet that definitely has more than two rows though. How am I going out of bounds of a two element array when my spreadsheet has 15 rows? How do I go about looping through these cells then to find their values?

Similar Threads

  1. Converting excel file to text file
    By saranya_v11 in forum New To Java
    Replies: 3
    Last Post: 07-05-2013, 10:22 AM
  2. ArrayIndexOutOfBounds, JTable.getValueAt
    By ZackO in forum AWT / Swing
    Replies: 10
    Last Post: 10-17-2010, 07:29 PM
  3. ArrayIndexOutofBounds Exception
    By atul.goldenstring in forum New To Java
    Replies: 10
    Last Post: 04-10-2010, 10:47 AM
  4. what's the token for excel file?
    By anthrax in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 07:19 AM
  5. ArrayIndexOutOfBounds
    By SwEeTAcTioN in forum New To Java
    Replies: 6
    Last Post: 12-07-2009, 12:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •