存储到2D阵列时,Java Array Index超出绑定异常

时间:2015-01-29 08:02:07

标签: java

我的Excel包含这样的值。

  1. 第1行:测试用例名称
  2. 第2行:标头参数值
  3. 第3行:IE的参数
  4. 第4行:Chrome的参数
  5. 第5行:Firefox的Parameater ..
  6. 我正在提取每个行值并将其存储在LinkedHashMap中。我正在创建一个2D数组并将此linkedHashmap存储到该2D数组中。

    它返回Index Out of Bound的异常。任何人都可以更正将表值存储到StoreAllArray

    package com.xchanging.selenium.utility;
    
    import java.io.IOException;
    import java.util.LinkedHashMap;
    
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    public class ReadExcel {
    
        public static LinkedHashMap<String, String> getData(String sheetName,
                String testCaseName) throws IOException {
            System.out.println("Entry Point");
            XSSFWorkbook sourceBook = new XSSFWorkbook("./TestCases.xlsx");
            XSSFSheet sourceSheet = sourceBook.getSheet(sheetName);
            int testCaseStartRow = 0;
            while (!sourceSheet.getRow(testCaseStartRow).getCell(0)
                    .getRichStringCellValue().toString().equals(testCaseName)) {
                testCaseStartRow++;
            }
            System.out.println("testCaseStartingRow: " + testCaseStartRow);
    
            int testCaseStartColumn = 0;
            int headerRow = testCaseStartRow + 1;
            int keyRow = headerRow + 1;
    
            while (sourceSheet.getRow(headerRow).getCell(testCaseStartColumn) != null) {
                testCaseStartColumn++;
            }
            int lastColumn = testCaseStartColumn - 1;
            // int parameters = lastColumn + 1;
            int numberofRows = keyRow;
            System.out.println("Keys Start Row" + keyRow);
            while (!sourceSheet.getRow(numberofRows).getCell(0).toString()
                    .equalsIgnoreCase("End")) {
                System.out.println(sourceSheet.getRow(numberofRows).getCell(0));
                numberofRows++;
            }
            System.out.println("numberofRows " + numberofRows);
            LinkedHashMap<String, String> table = new LinkedHashMap<String, String>();
            // Object[][] testData = new Object[0][lastColumn];
            Object[][] storeAllArray = new Object[0][3];
            int count = 0;
            for (int i = keyRow; i < numberofRows; i++) {
                for (int j = 0; j <= lastColumn; j++) {
                    int cellType = sourceSheet.getRow(i).getCell(j).getCellType();
                    if (cellType == 0) {
                        String key = sourceSheet.getRow(headerRow).getCell(j)
                                .toString();
                        String value = Double.toString(sourceSheet.getRow(i)
                                .getCell(j).getNumericCellValue());
                        table.put(key, value);
                    } else if (cellType == 1) {
                        String key = sourceSheet.getRow(headerRow).getCell(j)
                                .toString();
                        String value = sourceSheet.getRow(i).getCell(j).toString();
                        table.put(key, value);
    
                    } else if (cellType == 2) {
                        String key = sourceSheet.getRow(headerRow).getCell(j)
                                .toString();
                        String value = sourceSheet.getRow(i).getCell(j)
                                .getRawValue().toString();
                        table.put(key, value);
    
                    } else if (cellType == 4) {
                        String key = sourceSheet.getRow(headerRow).getCell(j)
                                .toString();
                        String value = Boolean.toString(sourceSheet.getRow(i)
                                .getCell(j).getBooleanCellValue());
                        table.put(key, value);
    
                    } else {
                        String key = sourceSheet.getRow(headerRow).getCell(j)
                                .toString();
                        String value = "";
                        table.put(key, value);
                    }
    
                }
                storeAllArray[0][count] = table;
                count++;
            }
            return table;
        }
    }
    
      

    线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:0     在   com.xchanging.selenium.utility.ReadExcel.getData(ReadExcel.java:81)     在   com.xchanging.selenium.utility.DataProviderConvertor.convertData(DataProviderConvertor.java:11)     在   com.xchanging.selenium.testcases.SanitySuite.main(SanitySuite.java:11)​​

2 个答案:

答案 0 :(得分:1)

Object[][] storeAllArray = new Object[0][3];是一个包含0行的数组。你不能放任何东西,这就是storeAllArray[0][count]失败的原因。

将其更改为:

Object[][] storeAllArray = new Object[1][3];

假设一行足够且count永远不会超过2.当然,如果只有一行,则不需要2D数组。

答案 1 :(得分:0)

import java.util.ArrayIndexOutofBondsException;
import java.util.Scanner;
public class Trycatch2
{
    public static void main(String[] args) {
        int a[] = {10,20,30};
        Scanner sc  = new Scanner(System.in);
        System.out.println("enter int index[0-2]");
        try
        {
            int index = sc.nextInt();
            System.out.println("element="+a[index]);
        }
        catch(ArrayIndexOutofBondsException e)
        {
            System.out.println("invalid int index using 0 as default index");
            System.out.println("elements="+a[0]);
        }
        System.out.println("bye");
        sc.close();
    }
}

    enter code here


show error