I am stuck at this error of null pointer exception. The error that i am receiving is : java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "this.driver" is null.
Please help !
Below is my code: ************** PAGE OBJECT CLASS************
package PageObjects;
import java.io.FileInputStream;
import java.time.Duration;
import java.util.List;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import StepDefinition.BaseClass;
import junit.framework.Assert;
public class WhatNewPOM{
WebDriver driver;
//constructor
public WhatNewPOM(WebDriver rdriver)
{
driver=rdriver;
PageFactory.initElements(rdriver, this);
}
@FindBy(xpath = "//*[@id='ui-id-3']")
WebElement whatsnewIcon;
@FindBy(xpath="//*[@class='categories-menu']/strong/span[contains(text(),'New in men')]")
WebElement newMen;
@FindBy(xpath="'//*[@class='categories-menu']/ul[2]/li")
List<WebElement> newMenCollection;
WebDriverWait wd= new WebDriverWait(driver, Duration.ofSeconds(20));
public void launchBrowser()
{
//setUp();
}
public void launchURL(String url)
{
driver.get(url);
}
public void whatsnewCategoryPresent()
{
wd.until(ExpectedConditions.visibilityOf(whatsnewIcon));
Assert.assertTrue(whatsnewIcon.isDisplayed());
}
public void clickOnWhatsNewIcon()
{
whatsnewIcon.click();
}
public void countAllElementsInMensCollection()
{
newMenCollection.size();
for(int i=0;i<newMenCollection.size();i++)
{
System.out.println(newMenCollection.get(i).getText());
}
}
}
STEP DEFINITION FILE
package StepDefinition;
import java.io.FileReader;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.support.PageFactory;
import PageObjects.WhatNewPOM;
import io.cucumber.java.Before;
import io.cucumber.java.en.*;
import io.github.bonigarcia.wdm.WebDriverManager;
public class WhatsNew extends BaseClass{
@Before
public void setUp()
{
Properties properties= new Properties();
wnp=new WhatNewPOM(driver);
try {
FileReader fis= new FileReader("E:\\Divya-Work\\eclipse_workspace\\CucumberDemo\\src\\test\\resources\\TestData.properties");
properties.load(fis);
} catch (Exception e) {
e.printStackTrace();
}
String browserName=properties.getProperty("browser");
System.out.println(browserName);
if(browserName.equalsIgnoreCase("chrome"))
{
WebDriverManager.chromedriver().setup();
driver= new ChromeDriver();
}
else
{
WebDriverManager.edgedriver().setup();;
driver= new EdgeDriver();
}
}
@Given("User lauches a browser")
public void user_lauches_a_browser() {
wnp.launchBrowser();
}
@When("User launches the {string}")
public void user_launches_the(String url) {
wnp.launchURL(url);
}
@Then("User should get WhatsNew catgory section")
public void user_should_get_whats_new_catgory_section() {
wnp.whatsnewCategoryPresent();
}
@When("User clicks on WhatsNew category")
public void user_clicks_on_whats_new_category() {
wnp.clickOnWhatsNewIcon();
}
@Then("User should count number of items present under mens section")
public void user_should_count_number_of_items_present_under_mens_section() {
wnp.clickOnWhatsNewIcon();
}
}
BASE CLASS
package StepDefinition;
import org.openqa.selenium.WebDriver;
import PageObjects.WhatNewPOM;
public class BaseClass {
public static WebDriver driver;
public WhatNewPOM wnp;
}
The problem comes from this line:
wnp=new WhatNewPOM(driver);
The driver
argument is here passed by-reference, which should be fine. But the by-reference passing does not work like dynamically binding to the driver
variable (static class attribute to be precise) forever, but to the object it is referring to at the moment of the call. You have not assigned anything to the driver
variable yet, so the actual object passed by-reference is null
. The later assignment
driver= new ChromeDriver();
does not change the object reference already passed to the WhatNewPOM
constructor. So the solution is to assign to the driver
variable before actually using it.