0

我遇到了这个 TestNG DataProvider 方法的问题。谁能告诉我为什么会发生这个错误并帮助我修复这个类?我无法插入数组。

我得到的错误是模糊的:

Caused by: java.lang.NullPointerException
    at tr.test.TestScript.createData(TestScript.java:55)

这是我的代码:

@SuppressWarnings("resource")
@DataProvider(name = "addresses")
public Object[][] createData() {
    Object[][] objs = new Object[100][];
    CSVReader reader = null;
    try {
        reader = new CSVReader( new FileReader("input.csv"), ',' );
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Object[] nextLine;
    int row = 0;
    try {
        while ( ( nextLine = reader.readNext() ) != null ) {
            System.out.println( "Adding test case " + (row+1) + ": " + nextLine[0] + ", " + nextLine[1] + ", " + nextLine[2] );
            objs[row][0] = nextLine[0]; //this is line #55
            objs[row][1] = nextLine[1];
            objs[row][2] = nextLine[2];
            row++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    if ( objs == null ) {
        System.out.println("Error: Input file empty.");
        System.exit(0);
    }
    return objs;
}
4

1 回答 1

0

因为你还没有初始化objs。就个人而言,我会构建一个 ArrayList 并且:

return list.toArray();

此外,您需要添加到列表中的项目也需要初始化:

ArrayList<Object[]> = new ArrayList<Object[]>();
...
Object[] foo = new Object[2];
foo[0] = nextLine[0];
foo[1] = nextLine[1];
foo[2] = nextLine[2];
System.out.println( "Adding test case " + (i+1) + ": " + a + ", " + c + ", " + s );
list.add(foo);

...
Object[][] objs = new Object[list.size()][];

for (int i = 0; i < objs.length; i++) {
    objs[i] = list.get(i);
}

return objs;
于 2012-07-19T06:37:40.240 回答