Getting null pointer exception : verifying the login successful or not.

public class Test {
        private static WebDriver driver; 
        private static XSSFWorkbook workbook;
        private static XSSFSheet sheet;
        private static XSSFCell cell;
        public void init() {
            System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
            driver= new ChromeDriver();
        public void setExcelFile() throws Exception  {
        File file =new File("TestData.xlsx");
        FileInputStream fis=new FileInputStream(file);
        workbook =new XSSFWorkbook(fis);
        sheet =workbook.getSheetAt(0);  
    public void login() {
        cell= sheet.getRow(1).getCell(0);
        DataFormatter dd=new DataFormatter();
        String username= dd.formatCellValue(cell);
        cell= sheet.getRow(1).getCell(1);
        String password=dd.formatCellValue(cell);
        Assert.assertTrue(driver.findElement(By.xpath("//div[contains(text(),'My Account')]")).isDisplayed());

console Error :

java.lang.NullPointerException: Cannot invoke "org.apache.poi.xssf.usermodel.XSSFSheet.getRow(int)" because "TestFlipkartDemo.sheet" is null


(1) When you create the TestFlipkartDemo object, the sheet pointer doesn't point to any object.

(2) Your test runner calls your test right after, resulting in you trying to call the method getRow of no object.

You must call the function at (3) before your test runner calls the function at (2).

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.