【问题标题】:Getting null pointer exception afer reading values from excel从excel读取值后获取空指针异常
【发布时间】:2018-01-22 02:38:23
【问题描述】:

正在正确读取 Excel 数据:

错误:info@gmail.com,1234444 info1@gmail.com,jjkkll;;jh [Utils] 尝试创建 C:\Users\priya\workspace\seleniumtopics\test-output\Default 套件\默认 test.xml [Utils] 目录 C:\Users\priya\workspace\seleniumtopics\test-output\Default 套件 存在:真 失败:输入数据(“info@gmail.com”,“1234444”) java.lang.NullPointerException 包pckg2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataInputfromExcel {
    public WebDriver driver;
    public WebDriverWait wait;
    @BeforeClass
    public void setup(){
        System.setProperty("webdriver.firefox.marionette","D:\\desktop\\Selenium\\geckodriver-v0.9.0-arm7hf\\geckodriver");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://mail.google.com");
         wait = new WebDriverWait(driver,20);
        //wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#account-chooser-add-account"))));
        //driver.findElement(By.cssSelector("#account-chooser-add-account")).click();
        }
    @Test(dataProvider="login")
    public void inputdata(String username,String password){
        System.out.println(username +","+password);
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#Email"))));
        driver.findElement(By.cssSelector("#Email")).sendKeys(username);
        driver.findElement(By.cssSelector("#next")).click();
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#Passwd"))));
        driver.findElement(By.cssSelector("#Passwd")).sendKeys(password);
        driver.findElement(By.cssSelector("#next")).click();
        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#errormsg_0_Passwd"))));
        String errortext=driver.findElement(By.cssSelector("#errormsg_0_Passwd")).getText();
        Assert.assertEquals(errortext,"Wrong password. Try again.");
    }

    @DataProvider(name="login")
    public Object[][] logindata() throws IOException{
        Object[][] arraydata=getexceldata("C:/Users/priya/Desktop/automationTopics.xlsx","Sheet3");
        return arraydata;

    }
    private String[][] getexceldata(String excelpath, String sheetname) throws IOException {
        String[][] exceldata=null;
        try {
            FileInputStream fs = new FileInputStream(excelpath);
            XSSFWorkbook wb = new XSSFWorkbook(fs);
            XSSFSheet sheet= wb.getSheet(sheetname);
            int rowcount=sheet.getLastRowNum()-sheet.getFirstRowNum();
            int colcount=sheet.getRow(0).getLastCellNum();
            System.out.println(rowcount +","+colcount);
            exceldata = new String[rowcount+1][colcount];
            for(int i=0;i<rowcount+1;i++){
                 Row row = sheet.getRow(i);
                for(int j=0;j<row.getLastCellNum();j++){
                    //exceldata[i][j]=sheet.getRow(i).getCell(j).getRichStringCellValue().getString();
                    DataFormatter formatter = new DataFormatter(); //creating formatter using the default locale
                     Cell cell = row.getCell(j);
                     exceldata[i][j] = formatter.formatCellValue(cell);
                }
            }


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        return exceldata;
    }

}

【问题讨论】:

    标签: selenium selenium-webdriver nullpointerexception


    【解决方案1】:

    您已在代码中声明了两次 driver,当调用 inputData 方法时,driver 对象未初始化。如果您仍然遇到同样的问题,请尝试关注并告诉我:

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.DataFormatter;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.testng.Assert;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;
    
    public class DataInputfromExcel {
        public WebDriver driver;
        public WebDriverWait wait;
        @BeforeClass
        public void setup(){
            System.setProperty("webdriver.firefox.marionette","D:\\desktop\\Selenium\\geckodriver-v0.9.0-arm7hf\\geckodriver");
            driver = new FirefoxDriver();
            driver.get("https://mail.google.com");
             wait = new WebDriverWait(driver,20);
            //wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#account-chooser-add-account"))));
            //driver.findElement(By.cssSelector("#account-chooser-add-account")).click();
            }
        @Test(dataProvider="login")
        public void inputdata(String username,String password){
            System.out.println(username +","+password);
            wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#Email"))));
            driver.findElement(By.cssSelector("#Email")).sendKeys(username);
            driver.findElement(By.cssSelector("#next")).click();
            wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#Passwd"))));
            driver.findElement(By.cssSelector("#Passwd")).sendKeys(password);
            driver.findElement(By.cssSelector("#next")).click();
            wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.cssSelector("#errormsg_0_Passwd"))));
            String errortext=driver.findElement(By.cssSelector("#errormsg_0_Passwd")).getText();
            Assert.assertEquals(errortext,"Wrong password. Try again.");
        }
    
        @DataProvider(name="login")
        public Object[][] logindata() throws IOException{
            Object[][] arraydata=getexceldata("C:/Users/priya/Desktop/automationTopics.xlsx","Sheet3");
            return arraydata;
    
        }
        private String[][] getexceldata(String excelpath, String sheetname) throws IOException {
            String[][] exceldata=null;
            try {
                FileInputStream fs = new FileInputStream(excelpath);
                XSSFWorkbook wb = new XSSFWorkbook(fs);
                XSSFSheet sheet= wb.getSheet(sheetname);
                int rowcount=sheet.getLastRowNum()-sheet.getFirstRowNum();
                int colcount=sheet.getRow(0).getLastCellNum();
                System.out.println(rowcount +","+colcount);
                exceldata = new String[rowcount+1][colcount];
                for(int i=0;i<rowcount+1;i++){
                     Row row = sheet.getRow(i);
                    for(int j=0;j<row.getLastCellNum();j++){
                        //exceldata[i][j]=sheet.getRow(i).getCell(j).getRichStringCellValue().getString();
                        DataFormatter formatter = new DataFormatter(); //creating formatter using the default locale
                         Cell cell = row.getCell(j);
                         exceldata[i][j] = formatter.formatCellValue(cell);
                    }
                }
    
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    
    
            return exceldata;
        }
    
    }
    

    在上面的代码中,我已经从 setup 方法中删除了对 driver 的重新声明,因为它已经被声明为类变量。此外,在您现有的代码中,setup 方法的 driver 变量仅在其方法中可见,即 setup。如果您还有任何疑问,请告诉我。

    【讨论】:

    • 谢谢..现在工作:-)
    • 太棒了。不客气。如果解决了您的问题,您能接受我的回答吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多