-1

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();
            driver.get("https://www.flipkart.com/");
            
        }
        
        public void setExcelFile() throws Exception  {
        File file =new File("TestData.xlsx");
        FileInputStream fis=new FileInputStream(file);
        workbook =new XSSFWorkbook(fis);
        sheet =workbook.getSheetAt(0);  
        }
    @Test
    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);
        driver.findElement(By.xpath("(//input[@type='text'])[2]")).sendKeys(username);
        driver.findElement(By.xpath("//input[@type='password']")).sendKeys(password);
        driver.findElement(By.xpath("(//button[@type='submit'])[2]")).click();
        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

0

(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.