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