-1

In Helpers class I have this below method

public void click(By locator){
    driver.findElement(locator).click();
}

In one of the pages used that method to click on an element as follows

public class HomePage {
    public  WebDriver driver;
    Locators locator = new Locators();
    //DriverSettings ds =new DriverSettings();
    Helpers helper; 
    
    public HomePage(WebDriver driver){
        this.driver=driver;
        helper= new Helpers(driver);
    }
    
    
    public void clickHomeLink(){
        
        helper.click(locator.HOMEPAGE_LINK_JAVA);
        
    }

Now in test.java class trying to click on click method is throwing a nullpointer exception which is pointing at com.functional.utilities.Helpers.click(Helpers.java:42) at com.functional.pages.HomePage.clickHomeLink(HomePage.java:31).

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

public class HomeJava extends BaseClass {
    
    private BaseClass baseobj = new BaseClass();
    public WebDriver driver;
    @Test
    public void testHomePage(){
        baseobj.getDriverSettings().StartBrowser("Chrome", "https://www.javatpoint.com/");
        baseobj.getHomePage().clickHomeLink();
    }
}

Can you please help me with this?

2
  • 1
    Where have you initialise HomePage object? Jan 29 at 18:03
  • what is your baseclass ? why have you added the homepage class when you are not calling that anywhere in your homejava !
    – PDHide
    Jan 29 at 20:57
1

Your base class should contain a driver initialization. Something like

public void setUp() {
    WebDriver driver = new ChromeDriver() // if you are using Chrome
}
1
  • Also you should mind the scope of that reference. In your example it would be only visible in scope of setUp() method
    – Alexey R.
    Jan 29 at 15:34

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.