在Testng中的org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:150)中显示java.lang.NullPointerException

时间:2016-02-03 13:26:48

标签: java selenium selenium-webdriver testng

我在使用CSVReader时显示错误

public class firstclass {
    private static WebDriver driver= new FirefoxDriver();

    @DataProvider(name="readcredentials")
    public Object[][] readcredentials() throws Exception{

        CSVReader readunamepass= new CSVReader(new FileReader("D:/My Projects  New/BSP/Aut/Credentials.csv"));
        List<String[]> list = readunamepass.readAll();  
        Iterator<String[]> readnext = list.iterator();

        while(readnext.hasNext())
        {

            String[] credential = readnext.next();
            System.out.println("" +readnext);
            System.out.println("username"+credential[0]+"password" +credential[1]);
            f(credential[0], credential[1]);

         }
        return null;
    }

    @Test(priority=1, dataProvider="readcredentials")
    public void f(String credential, String credential2) throws Exception {
        getDriver().get("http://192.168.1.111/lms");
        getDriver().findElement(By.id("txtUserId")).sendKeys( credential);
        getDriver().findElement(By.id("txtPwd")).sendKeys(credential2);
        getDriver().findElement(By.id("btnSign")).click();
        //Thread.sleep(100000);
        //System.out.println("This is method");
    }

    public WebDriver getDriver(){

        return driver;

    }

在这个程序中,@ dapaprovider正在从csv文件中读取uname和密码并将其传递给Test方法。 我无法提供返回类型,因此我提供了null.Please帮助 更多详情 - 我试图返回对象,但它向我展示了一些建议。我试过但在编译中显示错误。见图像Error image

Stacktrace

2 个答案:

答案 0 :(得分:1)

您的数据提供程序返回null,因此您将获得空指针异常

  

它应该返回Object[][]

此外,您不需要在readcredentials()方法中调用f(credential[0], credential[1])方法,而不是您的readcredentials()方法返回Object[][],在您的情况下,它可能是String [] [] 有关如何将数据提供程序与参数一起使用的更多详细信息,请参阅testng官方文档:Parameters with DataProviders

public class firstclass {


    @DataProvider(name="readcredentials")
    public Object[][] readcredentials() throws Exception{

        CSVReader readunamepass= new CSVReader(new FileReader("hello.csv"));
        List<String[]> list = readunamepass.readAll();
        String[][] array = new String[list.size()][];
        for(int i=0;i<list.size();i++)
        {
            array[i] = list.get(i);
        }
        return array;
    }

    @Test(priority=1, dataProvider="readcredentials")
    public void f(String value, String value1) throws Exception {
        System.out.println(value+" "+value1);
        //to do
        //your logic
    }
}

答案 1 :(得分:0)

所以终于得到了amitbhoraniya的正确答案,它对我有用。谢谢你,阿米特。 同时更新http://www.knowledgeworldforyou.com/?p=351 https://knowledgeworldforyou.wordpress.com/2016/02/04/unable-to-get-value-from-csvread-in-testng-here-is-solution-of-showing-java-lang-nullpointerexception-at-org-testng-internal-methodinvocationhelper-invokedataprovidermethodinvocationhelper-java150/

    public class firstclass {
    private static WebDriver driver= new FirefoxDriver();

    @DataProvider(name="readcredentials")
    public Object[][] readcredentials() throws Exception{

        CSVReader readunamepass= new CSVReader(new FileReader("D:/My Projects New/DSP/Aut/Credentials.csv"));
        List<String[]> list = readunamepass.readAll();
        String[][] array = new String[list.size()][];
        for(int i=0;i<list.size();i++)
        {
            array[i] = list.get(i);
        }
        return array;
    }

    @Test(priority=0, dataProvider="readcredentials")
    public void f1(String value, String value1) throws Exception {
        System.out.println(value+" "+value1);
        getDriver().get("http://192.168.1.128/lms");
        getDriver().findElement(By.id("txtUserId")).sendKeys(value);
        getDriver().findElement(By.id("txtPwd")).sendKeys(value1);
        getDriver().findElement(By.id("btnSign")).click();

        //to do
        //your logic
    }



  public WebDriver getDriver(){

  return driver;


 }




}