2015-11-05 254 views
0

在我嘗試將該字段值插入到UI文本框中時,在從Excel中讀取數據之後的以下代碼中,它將引發錯誤爲java.lang.nullpointerexception使用Apache poi從Excel讀取數據後發生java.lang.NullPointerException錯誤

在下面的代碼中,當我嘗試通過保留syst.out讀取Excel中的值時,它向我顯示正確的值。

我在這段代碼

 Cell=ExcelWSheet.getRow(1).getCell(0); 
     Cell1=ExcelWSheet.getRow(1).getCell(1); 

     String Celldata=Cell.getStringCellValue(); 
     String Celldata1=Cell1.getStringCellValue(); 
     System.out.println(Celldata); 
     System.out.println(Celldata1); 
     System.out.println("username value"); 
     Thread.sleep(2000); 
     ****driver.findElement(By.id("Email")).sendKeys(Celldata); 

我正在syst.out爲Celldata和Celldata1,面向然後當我試圖插入Celldata值到電子郵件領域正在此錯誤後問題。

我的完整代碼:

public class NewTest { 

public WebDriver driver; 

private static XSSFSheet ExcelWSheet; 

    private static XSSFWorkbook ExcelWBook; 

    private static XSSFCell Cell; 
    private static XSSFCell Cell1; 

    private static XSSFRow Row; 

@Test 
public void f() throws Exception { 
// This method is to set the file path and open the excel file 

    try{ 
    WebDriver driver=new FirefoxDriver(); 
    //driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
    driver.get("http://gmail.com"); 
    Thread.sleep(3000); 
    FileInputStream Excelfile=new FileInputStream("D:\\TestData\\Login.xlsx"); 
    ExcelWBook=new XSSFWorkbook(Excelfile); 
    ExcelWSheet=ExcelWBook.getSheet("Sheet1"); 

    } catch (Exception e){ 
     throw(e); 
    } 

// This code is used to read data from excel file 

    try{ 

     Cell=ExcelWSheet.getRow(1).getCell(0); 
     Cell1=ExcelWSheet.getRow(1).getCell(1); 

     String Celldata=Cell.getStringCellValue(); 
     String Celldata1=Cell1.getStringCellValue(); 
     System.out.println(Celldata); 
     System.out.println(Celldata1); 
     System.out.println("username value"); 
     Thread.sleep(2000); 
     ****driver.findElement(By.id("Email")).sendKeys(Celldata); 
     driver.findElement(By.id("next")).click(); 

     driver.findElement(By.id("Passwd")).sendKeys(Celldata1);**** 


    }catch (Exception e){ 
     throw(e); 
    } 

// This code is used to write data to the excel sheet 

    try{ 

     Row=ExcelWSheet.getRow(1); 
     Cell=Row.getCell(2,Row.RETURN_BLANK_AS_NULL); 
     if(Cell==null){ 

      Cell=Row.createCell(2); 
      Cell.setCellValue("Pass"); 

      FileOutputStream fileout=new FileOutputStream("Login.xlsx"); 
      ExcelWBook.write(fileout); 
      fileout.flush(); 
      fileout.close(); 
      }}catch(Exception e){ 
      throw(e); 


     } 

回答

0

在​​密碼輸入字段不存在一次。這就是爲什麼 driver.findElement(By.id("Passwd")).sendKeys(Celldata1); 引起的興趣。

填寫電子郵件地址後,可能需要一段時間才能顯示密碼字段。等待它最好的辦法是這樣的:

WebDriverWait wait = new WebDriverWait(driver, 30); //30s timeout 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd"))); 

如果你已經進入一個未知的電子郵件地址(Excel文件中)的口令字段不會出現在所有。在這種情況下,更改測試數據或在代碼中插入一些if語句。

相關問題