I have two calls as shown below in the @Test in the test class, one simply clicking on a webElement and other one performs an action to activate the dropdown(By simply hovering over using action.movetoelement) and then clicking on one of the options in that drop down.

My Program uses a POM with Page elements and the functions to be performed on those webelements in a separate class.

Package testcases;
import pageObjects.VisitHomePage;

—some code–

@Test
public void test1() {

login.eCommerceDemoSite();.// this throws a   

 login.clickEnrollyourself();// this works as expected

}
— some code


package pageObjects;

public class VisitHomePage extends Base {

By demoSitesLocator = By.xpath("/html/body/div[1]/div[1]/header/nav/ul/li[8]/a/span[1]/span/span"); //Locate Demo Site button/text on the toolsqa website
By eCommSiteLocator = By.xpath("/html/body/div[1]/div[1]/header/nav/ul/li[8]/ul/li[1]/a/span[2]"); //Locate E-Commerce website link in the demo sites drop down
By enrollYourSelf =  By.xpath("/html/body/div[1]/div[4]/div/div/ul/li/div[6]/div/div/div/a");

public VisitHomePage( driver) {
    super(driver);
    visit("some website");

}

public void clickEnrollyourself() {

    click(enrollYourSelf);

}
public void eCommerceDemoSite() {
          perform(demoSitesLocator);// this throws a null pointer exception
          click(eCommSiteLocator);

}

package pageObjects

public class Base{

private Actions action;
private WebDriver driver;

public Base(WebDriver driver) {
    this.driver =driver;

    }

public void perform(By locator) {
WebElement element = find(locator);
action.moveToElement(element).perform();//this throws a null pointer exception
}

public void click(By locator) {

    find(locator).click();// this works a expected
}

}

I have tried and failed to resolve this issue, Kindly advise



Source link https://sqa.stackexchange.com/questions/35447/why-is-one-function---a-null-pointer-exception-while-the-other-not

LEAVE A REPLY

Please enter your comment!
Please enter your name here