0

I want to store username and password in Excel whatever I will type in any application using WebDriver. I am using TestNG framework and Apache POI to write data to Excel. But I am getting Null pointer exception. Please tell me how to work WebDriver with Excel.

public class Test {

    private static WebDriver driver;
    static String username="abc";
    static String password="abf123";

    public static void main(String args[]) {
        try {
            driver.get("any url");

            driver.findElement(By.xpath("//*[@id='txtUserName']")).sendKeys(username);

            driver.findElement(By.xpath("//*[@id='txtPwd']")).sendKeys(password);

            FileOutputStream fos = new FileOutputStream("Userpass.xls");

            HSSFWorkbook workbook = new HSSFWorkbook();

            HSSFSheet worksheet = workbook.createSheet("POI WorkSheet");

            HSSFRow row1 = worksheet.createRow((short) 0);

            HSSFCell cell1 = row1.createCell((short) 0);

            cell1.setCellValue(username);

            HSSFCell cell2 = row1.createCell((short) 1);

            cell2.setCellValue(password);

            workbook.write(fos);

            fos.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();
        }
    }
    @Test
    public void f() {
    }
}
5
  • Where about are you getting the Null pointer? Commented Mar 18, 2013 at 10:47
  • when i debug my program i am getting Null pointer exception here.driver.get("any url"); Commented Mar 18, 2013 at 10:49
  • maybe try "WebDriver driver = new WebDriver();" not sure if that will solve the problem though. Commented Mar 18, 2013 at 10:54
  • 1
    I guess you need an instance of WebDriver (try "private static WebDriver driver = new WebDriver();"). Commented Mar 18, 2013 at 10:57
  • When i run my application with TestNG test it is not giving any error but excel sheet is not generating.Please help me what to do.. Commented Mar 18, 2013 at 11:56

1 Answer 1

1

Replace the following lines in your code

HSSFRow row1 = worksheet.createRow((short) 0);
HSSFCell cell1 = row1.createCell((short) 0);
cell1.setCellValue(username);
HSSFCell cell2 = row1.createCell((short) 1);
cell2.setCellValue(password);

with

HSSFRow row1 = worksheet.createRow(0);
row1.createCell(0).setCellValue(new HSSFRichTextString("username"));
row1.createCell(1).setCellValue(new HSSFRichTextString("password"));

you will get the desired output.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.