public class SearchCity {
public void tc() throws InterruptedException, IOException {
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("http://localhost/allmapview/");
Thread.sleep(3000);
ArrayList<String> data0 = readExcelData(0);
ArrayList<String> data1 = readExcelData(1);
for(int i=0; i<data0.size(); i++) {
driver.findElement(By.xpath(".//*[@id='from']")).sendKeys(data0.get(i));
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[@id='to']")).sendKeys(data1.get(i));
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[@id='calculate-route']/div[3]/div/div[2]/button")).click();
Thread.sleep(8000);
String strText = driver.findElement(By.id("google_dt")).getText();
String strText1 = driver.findElement(By.id("mmi_dt_2")).getText();
FileInputStream fis = new FileInputStream(new File("D:\\Screenshots\\Book1.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook (fis);
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.getRow(1);
XSSFCell cell = row.getCell(2);
cell.setCellValue(strText);
XSSFRow row2 = sheet.getRow(1);
XSSFCell cell2 = row2.getCell(3);
cell2.setCellValue(strText1);
fis.close();
FileOutputStream fos =new FileOutputStream(new File("D:\\\\Screenshots\\\\Book1.xlsx"));
workbook.write(fos);
fos.close();
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new
File("D:\\Screenshots\\Screen" +i));
Thread.sleep(2000);
driver.findElement(By.xpath(".//*[@id='calculate-route']/div[3]/div/div[3]/input")).click();
Thread.sleep(3000);
}
}
public ArrayList<String> readExcelData(int colNo) throws IOException {
File src = new File("D:\\Screenshots\\Book1.xlsx");
FileInputStream fis = new FileInputStream(src);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet1 = wb.getSheetAt(0);
Iterator<Row> rowIterator = sheet1.iterator();
rowIterator.next();
ArrayList<String> list = new ArrayList<String>();
while(rowIterator.hasNext()) {
list.add(rowIterator.next().getCell(colNo).getStringCellValue());
}
System.out.println("List :::"+list);
return list;
}
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
SearchCity city = new SearchCity();
city.tc();
我编写了一个代码来读取excel文件以填充网页中的一些条目,我想从该网页中获取一些数据并将其写入我的Excel工作表,但我的代码中有NullPointerException
我认为这不是一个重复的问题,所以请提出一些答案。
线程“main”中的异常java lang NullPointerException 在SearchCity SearchCity tc(SearchCity java:56) 在SearchCity SearchCity主页(SearchCity java:104)
答案 0 :(得分:-1)
首先,您在代码XSSFRow row = sheet.getRow(1);
和XSSFRow row2 = sheet.getRow(1);
中阅读了同一行。这不是必需的。然后,您需要检查sheet
是否为空,sheet.getRow(1)
是否更安全。接下来,读取行并检查单元格是否为null
,如果不是,则设置值。
这可能是您特定问题的解决方案,
if (sheet != null && sheet.getRow(1) != null) {
XSSFRow row = sheet.getRow(1);
if (row.getCell(2) != null) {
XSSFCell cell = row.getCell(2);
cell.setCellValue(strText);
}
if (row.getCell(3) != null) {
XSSFCell cell2 = row2.getCell(3);
cell2.setCellValue(strText1);
}
}