Kaushik
Kaushik

Reputation: 11

How to resolve NullPointerException

I am new to Automation Testing. I am using Cucumber, Page Object Model, Appium tools I cant figure it out where i am going wrong, its been a day since i am struck with this

This is my Base class

package Base;

public class Base {
        public static AndroidDriver<AndroidElement> driver;
        public static Properties prop;
        //static Reader reader;


        public static void base() throws InterruptedException, IOException{

        DesiredCapabilities cap = new DesiredCapabilities();
        try {
        prop = new Properties();
        FileInputStream ip;

        ip = new FileInputStream("/Users/user/Documents/Eclipse/Workspace/Project/src/test/java/Base/Base.properties");
        prop.load(ip);
        cap.setCapability("deviceName", prop.getProperty("deviceName"));
        //cap.setCapability("UDID", prop.getProperty("UDID"));
        cap.setCapability("platformName", prop.getProperty("platformName"));
        cap.setCapability("platformVersion", prop.getProperty("platformVersion"));
        cap.setCapability("automationName", prop.getProperty("automationName"));
        cap.setCapability("app", prop.getProperty("appPath"));
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

        try {
            driver = new AndroidDriver<AndroidElement> (new URL("http://0.0.0.0:4723/wd/hub/"),cap);
            Thread.sleep(2000);
            //driver.quit();
            }catch(IOException e) {
                e.printStackTrace();
            }

}

This is my Login Page

package PageObject;

public class loginPO extends Base{
    AndroidDriver<AndroidElement> driver;

    @AndroidFindBy(id ="com.project.coreapp:id/userField")
    WebElement userNameField;

    @AndroidFindBy(id="com.project.coreapp:id/test")
    WebElement passwordField;

    @AndroidFindBy(id="com.project.coreapp:id/loginButton")
    WebElement loginButton;

    //Initializing the page objects:
     public loginPO(AndroidDriver<AndroidElement> driver) {
     this.driver=driver;
     PageFactory.initElements(new AppiumFieldDecorator(driver), this);
     }


    public void typeUsername(String username) {
        userNameField.sendKeys(username);
    }

    public void typePassword(String password) {
        passwordField.sendKeys(password);
    }

    public void clickLogin() {
        loginButton.click();
    }

    public void login(String username, String password) {
    typeUsername(username);
    typePassword(password);
    clickLogin();

    //return new accountListPO(null);
    }

This is my Step Defenition

public class AccountOverviewQuickActions extends Base{

    loginPO log = new loginPO(driver);

    //accountListPO accountlist;

    @Given("^I am a Project customer$")
    public void i_am_a_project_customer() throws InterruptedException, IOException {
    Base.base();
    }

    @And("^I login and navigate to the Account List page$")
    public void i_login_and_navigate_to_the_Account_List_page() throws InterruptedException, IOException {
    //log.login("Hanna", "111111");

    log.login(prop.getProperty("userName"),prop.getProperty("passWord"));

    }

    @When("^I navigate to the Account Overview page$")
    public void i_navigateto_the_Account_Overview_page() throws InterruptedException {


    }

    @Then("^I should see two quick action buttons Details & Statement above the Transaction list$")
    public void i_should_see_two_quick_action_buttons_above_the_Transaction_list() {

    }

I am getting NullPointerException in the Step defenition @ here -

log.login(prop.getProperty("userName"),prop.getProperty("passWord"));

This is the Console output

org.openqa.selenium.WebDriverException: java.lang.NullPointerException
Build info: version: '3.141.59', revision: 'e567656', time: '2018-11-14T08:17:03'
System info: host: 'users-local', ip: xxxx', os.name: 'Mac OS X', os.arch: 'xxxx', os.version: '10.15.4', java.version: '1.8.0_241'
Driver info: driver.version: unknown
    at io.appium.java_client.pagefactory.AppiumElementLocator$WaitingFunction.apply(AppiumElementLocator.java:192)
    at io.appium.java_client.pagefactory.AppiumElementLocator$WaitingFunction.apply(AppiumElementLocator.java:1)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
    at io.appium.java_client.pagefactory.AppiumElementLocator.waitFor(AppiumElementLocator.java:99)
    at io.appium.java_client.pagefactory.AppiumElementLocator.findElement(AppiumElementLocator.java:119)
    at io.appium.java_client.pagefactory.interceptors.InterceptorOfASingleElement.intercept(InterceptorOfASingleElement.java:59)
    at org.openqa.selenium.remote.RemoteWebElement$$EnhancerByCGLIB$$d27c0df4.sendKeys(<generated>)
    at PageObject.loginPO.typeUsername(loginPO.java:38)
    at PageObject.loginPO.login(loginPO.java:50)
    at StepDefenition.AccountOverviewQuickActions.i_am_a_project_customer(AccountOverviewQuickActions.java:29)
    at ✽.I am a Santander bank SME customer(file:///Users/user/Documents/Eclipse/Workspace/project/src/test/resources/Feature/AccountOverviewQuickActions.feature:24)
Caused by: java.lang.NullPointerException
    at io.appium.java_client.pagefactory.AppiumElementLocator.lambda$0(AppiumElementLocator.java:120)
    at io.appium.java_client.pagefactory.AppiumElementLocator$WaitingFunction.apply(AppiumElementLocator.java:172)
    at io.appium.java_client.pagefactory.AppiumElementLocator$WaitingFunction.apply(AppiumElementLocator.java:1)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
    at io.appium.java_client.pagefactory.AppiumElementLocator.waitFor(AppiumElementLocator.java:99)
    at io.appium.java_client.pagefactory.AppiumElementLocator.findElement(AppiumElementLocator.java:119)
    at io.appium.java_client.pagefactory.interceptors.InterceptorOfASingleElement.intercept(InterceptorOfASingleElement.java:59)
    at org.openqa.selenium.remote.RemoteWebElement$$EnhancerByCGLIB$$d27c0df4.sendKeys(<generated>)
    at PageObject.loginPO.typeUsername(loginPO.java:38)
    at PageObject.loginPO.login(loginPO.java:50)
    at StepDefenition.AccountOverviewQuickActions.i_am_a_project_customer(AccountOverviewQuickActions.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at io.cucumber.java.Invoker.invoke(Invoker.java:27)
    at io.cucumber.java.JavaStepDefinition.execute(JavaStepDefinition.java:27)
    at io.cucumber.core.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:63)
    at io.cucumber.core.runner.TestStep.executeStep(TestStep.java:64)
    at io.cucumber.core.runner.TestStep.run(TestStep.java:49)
    at io.cucumber.core.runner.PickleStepTestStep.run(PickleStepTestStep.java:46)
    at io.cucumber.core.runner.TestCase.run(TestCase.java:51)
    at io.cucumber.core.runner.Runner.runPickle(Runner.java:67)
    at io.cucumber.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:149)
    at io.cucumber.junit.FeatureRunner.runChild(FeatureRunner.java:83)
    at io.cucumber.junit.FeatureRunner.runChild(FeatureRunner.java:24)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at io.cucumber.junit.Cucumber.runChild(Cucumber.java:185)
    at io.cucumber.junit.Cucumber.runChild(Cucumber.java:83)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at io.cucumber.junit.Cucumber$RunCucumber.evaluate(Cucumber.java:219)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210) 

Upvotes: 1

Views: 499

Answers (1)

UnknownBeast
UnknownBeast

Reputation: 979

Please do not extend loginPo and stepdef file with the Base. instead of that use any dependency injection like cucumber picocontainer to share the state of base class with other java files.

Read about it more here: http://www.thinkcode.se/blog/2017/04/01/sharing-state-between-steps-in-cucumberjvm-using-picocontainer

Upvotes: 1

Related Questions