尝试从secelium webdriver的excel读取数据时出现ArrayIndexOutOfBoundsException

时间:2016-02-28 13:03:40

标签: java selenium

我正在尝试读取一个包含用户名和密码列表的excel文件。 我得到" java.lang.RuntimeException:java.lang.ArrayIndexOutOfBoundsException:2"

我的Excel看起来像这样

用户名密码--- U1 P1

U2 P2

U3 P3

U4 P4

意思是,它有2列5行。我不希望第一行被计入,所以我给了Object data [] [] = new Object [rows-1] [cols];

请协助解决问题。谢谢,

package com;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class LoginTest {

    @Test(dataProvider="getData")
    public void doLogin(String userName, String password){


    }
    @DataProvider
    public Object[][] getData(){

        Xls_Reader xls = new Xls_Reader("E:\\Pessoal\\QTPSelenium\\Excel\\Login.xlsx");
        int rows = xls.getRowCount("Login");
        System.out.println("Row Count---"+rows);
        int cols = xls.getColumnCount("Login");
        System.out.println("Column Count---"+cols);

        Object data[][] = new Object[rows-1][cols];

        for(int rNum=2;rNum<=rows;rNum++){
            for(int cNum=0;cNum<=cols;cNum++){
                System.out.println(xls.getCellData("Login", cNum, rNum));
//ERROR IN THIS LINE
                    data[rNum][cNum] = xls.getCellData("Login", cNum, rNum);
                }
            }
            return data;
        }
    }

我在这一行得到错误

                data[rNum][cNum] = xls.getCellData("Login", cNum, rNum);

2 个答案:

答案 0 :(得分:0)

试试这个

for(int rNum=1;rNum<rows;rNum++){
            for(int cNum=0;cNum<cols;cNum++){
                System.out.println(xls.getCellData("Login", rNum, cNum));
                    data[rNum-1][cNum] = xls.getCellData("Login", rNum, cNum);
                }
            }

答案 1 :(得分:0)

“ for(int cNum = 0; cNum <= cols; cNum ++)”

请将“ cNum <= cols”更改为“ cNum