第一行插入成功,当第二行插入excel获取NullPointerException时

时间:2017-08-18 05:05:05

标签: java selenium-webdriver apache-poi

import java.io.FileInputStream;
import java.io.FileOutputStream;    
import java.io.IOException;    
import java.util.List;    
import java.util.concurrent.TimeUnit;

import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
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.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class CityBusRoutes {

    static String cityName;
    static String routeNo;
    static String routeDetails;

    static String routeList;

    public static void main(String[] args) throws IOException {

        // TODO Auto-generated method stub

        System.setProperty("webdriver.chrome.driver",
                "D://Selenium//Selenium Drivers//chromedriver_win32//chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.get("http://www.onefivenine.com/busRoute.dont?method=findBusRoute");

        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

        FileInputStream fis = new FileInputStream("D://City bus routes task//routes-list.xlsx");
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        XSSFSheet sh = wb.getSheetAt(0);

        int totalNoOfRows = sh.getLastRowNum();
        System.out.println("Row count :"+totalNoOfRows);

        for (int i = 1; i < totalNoOfRows; i++) {

            XSSFRow row = sh.getRow(i);
            cityName = new DataFormatter().formatCellValue(row.getCell(0));
            routeNo = new DataFormatter().formatCellValue(row.getCell(1));

            System.out.println("route opened");

            Select ddl1 = new Select(driver.findElement(By.id("cityId")));

            ddl1.selectByVisibleText(cityName);

            Select ddl2 = new Select(driver.findElement(By.xpath(".//*[@id='routeId']")));
            ddl2.selectByVisibleText(routeNo);

            System.out.println("route selected");

            routeDetails=driver.findElement(By.xpath("html/body/table/tbody/tr[3]/td[2]/div[2]/table[2]")).getText();
            routeList=driver.findElement(By.xpath("html/body/table/tbody/tr[3]/td[2]/div[2]/table[3]/tbody/tr/td[1]/table")).getText();

                row.getCell(2).setCellValue(routeDetails);
                row.getCell(3).setCellValue(routeList);

                FileOutputStream fos = new FileOutputStream("D://City bus routes task//routes-list.xlsx");

                wb.write(fos);
                fos.close();
        }
    }   
}

以下是我的StackTrace:

Row count :2668
route opened
route selected
route opened
route selected
Exception in thread "main" java.lang.NullPointerException
at CityBusRoutes.main(CityBusRoutes.java:100)

读取第一行中的前两个单元格并在第一行中写下两个单元格,
读取第二行中的前两个单元格也会发生,但写入第二行并未发生,并显示上述错误。

0 个答案:

没有答案