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
    }
}
🌐
Wordpress
https://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…
🌐
Stack Exchange
https://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.
🌐
Stack Overflow
https://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
🌐
qtpselenium
https://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 . 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 and UFT/QTP training.
Top answer
1 of 2
2

Initialisers are invoked before constructors and hence, NullPointerException, try the below code:

public class BaseClass{

  public static WebDriver driver;

  public BaseClass(WebDriver _driver){
      driver = _driver;
      FirstName = driver.findElement(By.name("firstname"));
      LastName  = driver.findElement(By.name("LastName"));
  }

  public  WebElement FirstName;
  public  WebElement LastName;
  //Rest of the code
2 of 2
1

@DarshanMehta has well explained the problem and its solution. I have just tested it and it works. I would also add:

The problem you are facing has been well discussed in Stackoverflow. I think this link can give you a better understanding of the order of execution when a class is instanciated: Are fields initialized before constructor code is run in Java?

Thus you can read in the first answer that initialization block is done after the super() part of the constructor, BUT before the rest of the constructor.

you would not have had this error if, for exemple, your driver was initialised in a superclass of BaseClass. Therefore, if you really want to initialized FirstNameand LastName in initialization block, you can create a superclass from which BaseTest will inherits. And it should solve your issue.

you can try the following code, that illustrates it: create a super class:

public class MotherClass {
    public MotherClass(WebDriver _driver){
        driver = _driver;
    }

    WebDriver driver;
}

and add super in the constructor of BaseClass, that inherits from MotherClass:

public class Testasupp extends MotherClass{    
    public BaseClass(WebDriver _driver)
        {
            super(_driver);
            FirstName = driver.findElement(By.name("firstname"));
            LastName  = driver.findElement(By.name("LastName"));
        }

To conclude: Except if you have good reason to prefer this solution, I really think @DashanMehta gives a better solution.

🌐
Stack Overflow
https://stackoverflow.com › questions › 60832706 › how-to-solve-null-pointer-exception-of-webdriver-in-selenium
java - How to solve null pointer exception of webdriver in selenium - Stack Overflow
March 24, 2020 - However it shows me a null pointer exception for the webdriver. i have also printed "driver". it prints "drivernull" *package demo; import static org.junit.jupiter.api.Assertions.*; import java.util.concurrent .TimeUnit; import org.junit.Before; import org.junit.jupiter.api.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.remote.DesiredCapabilities; class Demo1 { public WebDriver driver; String url="www.google.com"; @Before public void before() { System.setProperty("webdriver.chrome.driver", "D
🌐
Stack Overflow
https://stackoverflow.com › questions › 43415676 › java-selenium-null-pointer
java selenium null pointer - Stack Overflow
CopyOnly local connections are allowed. Exception in thread "main" java.lang.NullPointerException at Flight.getPageProperties(Flight.java:27) at Flight.main(Flight.java:47)
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;
}