You need to initialize your WebDriver first. As I seeing you are creating the reference of WebDriver but not initialized, try as below :-

WebDriver driver = new ChromeDriver(); //or other driver which you want

Note :- If you want to initialize ChromeDriver you need to download chromedriver and set system property before initialize driver as :-

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

So details answer like as below :-

public class VendorsHomePageTest 

{
    public WebDriver driver;
    @Test
    public void verifyVendorsHomePageTest() throws Exception
    {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver(); //or other driver which you want

        LoginIntoVendors login=PageFactory.initElements(driver, LoginIntoVendors.class);

        login.verifyLoginVendors();

        VendorsHomePageApp vhpapp= PageFactory.initElements(driver, VendorsHomePageApp.class);

        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        Thread.sleep(3000);

        vhpapp.clickOnNotificationsTab();

        vhpapp.clickOnProfileTab();

        vhpapp.clickOnTendersTab();

        vhpapp.clickOnDashboardTab();

        vhpapp.clickOnFirstTender();

    }

}

Edited :- You need to create separate class which gives you WebDriver instance as below :-

public class DriverInit {

    public WebDriver driver;
    private static DriverInit driverInit = null;

    public static DriverInit getInstance() {
        if (driverInit == null) {
            driverInit = new DriverInit();
        }
        return driverInit;
    }

    private DriverInit() {
        this.driver = new FirefoxDriver();
        this.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        this.driver.get("******");
    }

    public WebDriver getDriver() {
        return this.driver;
    }

now you can call it into LoginIntoVendors as below :-

public class LoginIntoVendors 
{
    @Test
       public void verifyLoginVendors() throws Exception
       {
           WebDriver driver = DriverInit.getInstance().getDriver();

           IntiationPage vendorInit=PageFactory.initElements(driver, IntiationPage.class);

           vendorInit.clickOnLoginButton();

           VendorsLoginAction Loginven=PageFactory.initElements(driver, VendorsLoginAction.class);

           Loginven.vendorlogin("***","***@gmail.com", "****");

           String title = driver.getTitle();
           System.out.println("Title is  :" + title  );


       }
}

and in the VendorsHomePageTest as below :-

public class VendorsHomePageTest 

{
    public WebDriver driver;
    @Test
    public void verifyVendorsHomePageTest() throws Exception
    {

        driver = DriverInit.getInstance().getDriver();

        LoginIntoVendors login=PageFactory.initElements(driver, LoginIntoVendors.class);

        login.verifyLoginVendors();

        VendorsHomePageApp vhpapp= PageFactory.initElements(driver, VendorsHomePageApp.class);

        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        Thread.sleep(3000);

        vhpapp.clickOnNotificationsTab();

        vhpapp.clickOnProfileTab();

        vhpapp.clickOnTendersTab();

        vhpapp.clickOnDashboardTab();

        vhpapp.clickOnFirstTender();

    }

}

Hope it helps..:)

Answer from Saurabh Gaur on Stack Overflow
Top answer
1 of 2
1

You need to initialize your WebDriver first. As I seeing you are creating the reference of WebDriver but not initialized, try as below :-

WebDriver driver = new ChromeDriver(); //or other driver which you want

Note :- If you want to initialize ChromeDriver you need to download chromedriver and set system property before initialize driver as :-

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

So details answer like as below :-

public class VendorsHomePageTest 

{
    public WebDriver driver;
    @Test
    public void verifyVendorsHomePageTest() throws Exception
    {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver(); //or other driver which you want

        LoginIntoVendors login=PageFactory.initElements(driver, LoginIntoVendors.class);

        login.verifyLoginVendors();

        VendorsHomePageApp vhpapp= PageFactory.initElements(driver, VendorsHomePageApp.class);

        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        Thread.sleep(3000);

        vhpapp.clickOnNotificationsTab();

        vhpapp.clickOnProfileTab();

        vhpapp.clickOnTendersTab();

        vhpapp.clickOnDashboardTab();

        vhpapp.clickOnFirstTender();

    }

}

Edited :- You need to create separate class which gives you WebDriver instance as below :-

public class DriverInit {

    public WebDriver driver;
    private static DriverInit driverInit = null;

    public static DriverInit getInstance() {
        if (driverInit == null) {
            driverInit = new DriverInit();
        }
        return driverInit;
    }

    private DriverInit() {
        this.driver = new FirefoxDriver();
        this.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        this.driver.get("******");
    }

    public WebDriver getDriver() {
        return this.driver;
    }

now you can call it into LoginIntoVendors as below :-

public class LoginIntoVendors 
{
    @Test
       public void verifyLoginVendors() throws Exception
       {
           WebDriver driver = DriverInit.getInstance().getDriver();

           IntiationPage vendorInit=PageFactory.initElements(driver, IntiationPage.class);

           vendorInit.clickOnLoginButton();

           VendorsLoginAction Loginven=PageFactory.initElements(driver, VendorsLoginAction.class);

           Loginven.vendorlogin("***","***@gmail.com", "****");

           String title = driver.getTitle();
           System.out.println("Title is  :" + title  );


       }
}

and in the VendorsHomePageTest as below :-

public class VendorsHomePageTest 

{
    public WebDriver driver;
    @Test
    public void verifyVendorsHomePageTest() throws Exception
    {

        driver = DriverInit.getInstance().getDriver();

        LoginIntoVendors login=PageFactory.initElements(driver, LoginIntoVendors.class);

        login.verifyLoginVendors();

        VendorsHomePageApp vhpapp= PageFactory.initElements(driver, VendorsHomePageApp.class);

        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        Thread.sleep(3000);

        vhpapp.clickOnNotificationsTab();

        vhpapp.clickOnProfileTab();

        vhpapp.clickOnTendersTab();

        vhpapp.clickOnDashboardTab();

        vhpapp.clickOnFirstTender();

    }

}

Hope it helps..:)

2 of 2
0

Your driver seems to be null what you can do to avoid this situation is to instantiate your driver reference.

In your test class you can instantiate your driver.

public class VendorsHomePageTest {
    public WebDriver driver;
    @Test
    public void verifyVendorsHomePageTest() throws Exception {
        driver = new FirefoxDriver(); // or whatever browser you want.
        // Here you can initialise the elements in `PageObject` class.
        VendorsHomePageApp vhpapp= PageFactory.initElements(driver, VendorsHomePageApp.class);
        // Rest of your code
    }
}
Discussions

Java selenium - null pointer exception on webDriver inheritance - Software Quality Assurance & Testing Stack Exchange
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I am getting null pointer exception. More on sqa.stackexchange.com
🌐 sqa.stackexchange.com
Getting null pointer exception | Selenium Forum
Additionally, null pointer exception comes up when the object is not accessible. Share your project in a zip file by exporting it if debugging doesn't help. ... HI I tried debugging the code . I declared : WebDriver driver= new ChromeDriver(); just before create portfolio test and it worked. ... you need to write the below line only, else the driver won't be accessible to other functions. driver = new ChromeDriver(); We offer online selenium ... More on qtpselenium.com
🌐 qtpselenium.com
Java-Null Pointer exception in selenium - Stack Overflow
When I try to run my POM(page object model) for my login page using Data Driven Framework, my application has been launched and data has been read but getting NullPointerException. The data is not... More on stackoverflow.com
🌐 stackoverflow.com
Null Pointer Exception in Selenium WebDriver - Stack Overflow
I am using the below code to click on navigation links one by one till it reaches end by using WebDriver but it throw NullPointerException, confused as I have already initialized and still facing t... More on stackoverflow.com
🌐 stackoverflow.com
April 17, 2014
🌐
Stack Exchange
sqa.stackexchange.com › questions › 51579 › java-selenium-null-pointer-exception-on-webdriver-inheritance
Java selenium - null pointer exception on webDriver inheritance - Software Quality Assurance & Testing Stack Exchange
I am getting null pointer exception. Would you be able to help and what should be fixed ? java.lang.NullPointerException at pages.LandingPage.acceptConsent(LandingPage.java:14) at stepDefs.
🌐
Wordpress
learnseleniumwebdrivercom.wordpress.com › 2017 › 08 › 10 › java-lang-nullpointerexception-in-selenium
Java Lang NullPointerException in Selenium – Learn Selenium Webdriver
February 21, 2018 - Java Lang NullPointerException in Selenium,While running the selenium test scripts you'll face some challenges with the test scripts,in that challenges this is the main important exception,not able to understand why it is displaying and most of the time you'll think that it is raised because of element is not identifying by the Selenium,that is not…
🌐
qtpselenium
qtpselenium.com › selenium-training › forum › getting-null-pointer-exception-3547
Getting null pointer exception | Selenium Forum
Additionally, null pointer exception comes up when the object is not accessible. Share your project in a zip file by exporting it if debugging doesn't help. ... HI I tried debugging the code .
🌐
Stack Overflow
stackoverflow.com › questions › 49646592
Java-Null Pointer exception in selenium - Stack Overflow
Below is my exception, please help me out but the same code runs properly in my colleague system. FAILED: entercredentials("3019", "India@123") java.lang.NullPointerException at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69) at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38) at com.sun.proxy.$Proxy9.sendKeys(Unknown Source) at PageLibrary.LoginPageNew.Credential(LoginPageNew.java:51) at Test.ValidLogin.entercredentials(ValidLogin.java:54) at sun.reflect.NativeMethodAccessorI
Find elsewhere
🌐
Quora
quora.com › Why-do-we-get-a-null-pointer-exception-in-Selenium
Why do we get a null pointer exception in Selenium? - Quora
Answer (1 of 2): NullPointerException is a runtime exception and it is thrown when the application try to use an object reference which has a null value.Some of the common reasons for NullPointerException in java programs are: 1. Invoking methods on an object which is not initialized 2. Paramete...
🌐
Katalon
forum.katalon.com › web testing
I am getting Null Pointer exception when using the selenium script in katalon recorder using interlliJ - Web Testing - Katalon Community
December 7, 2023 - Please check and let me know what have to change. package SpinConnect; import io.github.bonigarcia.wdm.WebDriverManager; import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import java.util.concurrent.TimeUnit; import static org.testng.Assert.fail; import java.util.concurrent.TimeUnit; import org.testng.annotations.; import static org.testng.Assert.; import org.openqa.seleni...
🌐
GitHub
github.com › SeleniumHQ › selenium › issues › 4555
Null pointer exception when calling webDriver.findElements · Issue #4555 · SeleniumHQ/selenium · GitHub
August 23, 2017 - You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. ... There was an error while loading. Please reload this page. ... Occasionally, calling webDriver.findElements(By.cssSelector(query)) causes a null pointer exception. Here is the stack trace: java.lang.NullPointerException at org.openqa.selenium.remote.RemoteWebDriver.findElements(RemoteWebDriver.java:450) at org.openqa.selenium.remote.RemoteWebDriver.findElementsByCssSelector(RemoteWebDriver.java:509) at org.openqa.selenium.By$ByCssSelector.
Author: SeleniumHQ
Top answer
1 of 2
1

As I understood, you are trying to read each row's data from the excel file and passing them to runTest(row.getCell(1).toString(),row.getCell(2).toString()); method

so here is your complete code to do that -

try {
        // Open the Excel file
        FileInputStream fis = new FileInputStream("D:\\Workspace\\test\\src\\test\\testdata.xls");
        // Access the required test data sheet
        HSSFWorkbook wb = new HSSFWorkbook(fis);
        HSSFSheet sheet = wb.getSheet("testdata");
        int rowNumber=0;

         Iterator<Row> rowIterator = sheet.iterator();

            // Traversing over each row of XLSX file
                while (rowIterator.hasNext())
                {                       
                    System.out.println("Running test case "+sheet.getRow(rowNumber).getCell(0).getStringCellValue());
                // Run the test for the current test data row
                runTest(sheet.getRow(rowNumber).getCell(0).toString(),sheet.getRow(rowNumber).getCell(1).toString());

                rowNumber++;
              // To check row cell is not empty
               if(sheet.getRow(rowNumber)==null)
                {
                     break;
                }
            }

         wb.close();
        fis.close();
    } 
catch (IOException e) 
{
        System.out.println("Test data file not found");
   } 
2 of 2
0

You for should start from 0 and not 1 but you could try using iterator

Adjust following code to your needs

try {
    // Open the Excel file
    FileInputStream fis = new FileInputStream("D:\\Workspace\\test\\src\\test\\testdata.xls");
    // Access the required test data sheet
    HSSFWorkbook wb = new HSSFWorkbook(fis);
    HSSFSheet sheet = wb.getSheet("testdata");

    Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) { // cycle throuh all rows
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            if (cellIterator.hasNext()) { //change to while if you need all columns, otherwise if you need the first column leave as it is...
                Cell cell = cellIterator.next();
                System.out.println("Running test case " + cell.getStringCellValue());
            }
        }
    }            
} catch (IOException e) {
    System.out.println("Test data file not found");
}   
finally
{
    if (fis != null)
    fis.close();
}

You can also check the type of a column using:

String tmp = "";
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
    tmp = cell.getNumericCellValue() + "";
    break;
case Cell.CELL_TYPE_STRING:
    tmp = cell.getStringCellValue();
    break;
// case Cell.<other types>: ... ; break;
}
🌐
Stack Overflow
stackoverflow.com › questions › 49921957 › null-pointer-exception-in-selenium-framework
automation - Null pointer exception in selenium framework - Stack Overflow
In third class am calling the first 2 method...When I run this class(Test1),The first method execution is happening when it goes to the second one(Levelmanage.managelink(driver).click();) driver is getting NULL.
🌐
Stack Overflow
stackoverflow.com › questions › 39974292 › i-am-getting-null-pointer-exception-in-my-selenium-script
java - I am getting Null Pointer Exception in my Selenium Script? - Stack Overflow
June 12, 2017 - Exception in thread "main" java.lang.NullPointerException at ArrayLiist.main(ArrayLiist.java:13) java · selenium · Share · Improve this question · Follow · asked Oct 11, 2016 at 9:31 · Neeraj RathorNeeraj Rathor · 1 1 · 2 · "i have institiated webdriver =null , still it is showing null pointer exception" Well, yes...