2017-02-04 23 views
0

Excel中的數據越來越正確讀取:獲得空指針異常AFER從Excel中讀取值

Error:[email protected],1234444 [email protected],jjkkll;;jh [Utils] Attempting to create C:\Users\priya\workspace\seleniumtopics\test-output\Default suite\Default test.xml [Utils] Directory C:\Users\priya\workspace\seleniumtopics\test-output\Default suite exists: true FAILED: inputdata("[email protected]", "1234444") java.lang.NullPointerException package 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; 
    } 

} 

回答

0

您已經聲明司機兩次在你的代碼,當被調用inputData方法,驅動程序對象未初始化。試試下面,讓我知道,如果你仍然面臨着同樣的問題:

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; 
    } 

} 

在上面的代碼,我從設置方法去除驅動重新聲明,因爲它已經被聲明爲類變量。此外,在您現有的代碼中,驅動程序變量設置方法僅在其方法中具有可見性,即設置。讓我知道,如果你有任何進一步的疑問。

+0

謝謝..現在工作:-) –

+0

太好了。不用謝。如果能解決您的問題,您能否接受我的回答? – Mahipal