-2
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();

I have written a code to read an excel file to fill some entries in a webpage and I want to fetch some data from that webpage and write it into my excel sheet, but I got NullPointerException in my code at different lines, and it is not a duplicate question I think, so please suggest some answers.

Exception in thread "main" java lang NullPointerException at SearchCity SearchCity tc(SearchCity java:56) at SearchCity SearchCity main(SearchCity java:104)

VPK
  • 3,010
  • 1
  • 28
  • 35
shivam
  • 249
  • 1
  • 5
  • 18
  • @VPK its `city.tc();` line 56 is more important – XtremeBaumer Jan 29 '18 at 09:39
  • i guess the problem is around here `FileOutputStream fos =new FileOutputStream(new File("D:\\\\Screenshots\\\\Book1.xlsx")); workbook.write(fos); fos.close();` – XtremeBaumer Jan 29 '18 at 09:40
  • @shivam, your exception is basically thrown at line `56`. What is the code at that line? Please check by debugging if you are trying to access the elements on `null` object. – VPK Jan 29 '18 at 09:45
  • cell.setCellValue(strText); this is the line 56 – shivam Jan 29 '18 at 09:48
  • suggest me an solution – shivam Jan 29 '18 at 10:02
  • This means, `cell` is `null`. Try to debug if the row has the cell at index `2`. You can provide a check using `if` statement like - `if (row.getCell(2) != null)` and then do the corresponding operations. – VPK Jan 29 '18 at 10:04
  • yes you are right, so what is the solution for that? – shivam Jan 29 '18 at 10:21
  • @shivam, have you tried the below solution? – VPK Jan 29 '18 at 11:27

1 Answers1

-1

First thing, you are reading the same row twice in your code XSSFRow row = sheet.getRow(1); and in XSSFRow row2 = sheet.getRow(1);. This is not required. Then, you need to check whether the sheet is null and sheet.getRow(1) to be on safer side. Next, read the row and check if the cells are null, if not, then set the value.

This could be the solution to your particular problem,

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);
    }
}
VPK
  • 3,010
  • 1
  • 28
  • 35
  • code is running fine but excel file is not updated with strText and strText1 – shivam Jan 29 '18 at 11:31
  • Try to debug the code and check if the rows have cells. If the cells are null, value could not be updated. – VPK Jan 29 '18 at 11:45
  • @shivam, by the way, it would be a different question as to why the file is not being updated. If your initial problem of `NullPointerException` has solved, you can mark the answer as Accepted. – VPK Jan 29 '18 at 12:01
  • You shouldn't have to check if every single workbook access is `null`. – JeffC Jan 29 '18 at 15:16
  • @JeffC, it's just for `to be on the safer side` I said in the answer. However, how are you supposed to avoid `NPE`? – VPK Jan 30 '18 at 04:14