1

I have 3 classes - one for page locator, one for page action and the other one as script to execute the function. am getting nullpointer exception in main scripts where the function is called. Can anyone help me out, please!!!!.

The following are the code :

  1. HomePageLocator.page

public class HomePageLocator{ WebDriver driver; public HomePageLocator(WebDriver driver) { this.driver= driver; }

    @FindBy(xpath="//*[@id='header']/div[2]/div/div/nav/div[1]/a")
    public WebElement signIn;

}

  1. HomePageAction.page public class HomePageAction{

    public WebDriver driver; public HomePageLocator homepageor;

    public HomePageAction() {

    this.homepage = new HomePageLocator(driver);
    PageFactory.initElements(driver, this.homepage);
    

    }

    public void login() { homepageor.signIn.click(); }

  2. BaseTestCase.java

public class BaseTestCase {

public static Logger log = Logger.getLogger("devpinoyLogger");

public static void main(String[] args) throws Throwable  {

    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\src\\test\\resources\\Executables\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://automationpractice.com/index.php");
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    // Home page validation
    HomePageAction homepageaction= new HomePageAction();
    homepageaction.login();


}

Note : Am getting exception in line (homepageaction.login();) the following is the exception logs : Exception in thread "main" 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.$Proxy3.click(Unknown Source) at com.way2.Pages.actions.HomePage.login(HomePageAction.java:31) at com.way2.Testcases.BaseTestCase.main(BaseTestCase.java:35)

1
  • Could you please properly format your code. Commented Jul 12, 2019 at 20:24

1 Answer 1

0

You are creating the driver in main class, but not passing it to homepageAction public static void main(String[] args) throws Throwable {

System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\src\\test\\resources\\Executables\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

Try to pass driver as HomePageAction homepageaction= new HomePageAction(driver); this.driver =driver

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.