IntelliJ error: java.lang.NullPointerException

2.9k views Asked by At

I use selenium, WebDriver, Intellij, Junit4, ChromeDriver, PageObject, PageFactory.

I followed the instructions here: https://www.youtube.com/watch?v=pO5Ry_GLC60&t=582s Project works when you follow this video. After the video I used PageFactory. I'm instead of Abstract class I wrote BasePage class, I made a new class that contains @ Before, @ After and method for webdriver. When I run the test error is : java.lang.NullPointerEkception. I do not know how to solve the problem. Help please.

This is my HomePage page:

package PageObjectPage;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class HomePage extends BasePage {

    @FindBy(how = How.NAME, using = "account_icon")
    @CacheLookup
    WebElement button_my_accout;


    public HomePage(WebDriver driver){

        super(driver);
    }

   public MyAccount clickOnMyAccount(){
        //Click on My Account
        button_my_accout.click();

       return PageFactory.initElements(getDriver(), MyAccount.class);
    }

    }

This is MyAccount page:

package PageObjectPage;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;


public class MyAccount extends BasePage {

    @FindBy(id = "log")
    @CacheLookup
    WebElement username;

    @FindBy(how = How.ID, using = "pwd")
    @CacheLookup
    WebElement password;

    @FindBy(how = How.ID, using = "login")
    @CacheLookup
    WebElement login_button;


    public MyAccount(WebDriver driver){

        super(driver);
    }

    public MyAccount LogIn(){
        //Fill in the text box username
        username.sendKeys("Dragana");
        //Fill in the text box password
        password.sendKeys("123456");

        return new MyAccount(driver);
    }
    public LogInResultPage submitForm() {
        //Click on button Log in
        login_button.click();

        return new LogInResultPage(driver);
    }
}

This is LogInResultPage:

package PageObjectPage;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;


public class LogInResultPage extends BasePage{

    public LogInResultPage(WebDriver driver){

        super(driver);
    }
    public String getMessage(){
        //Printing message
        return driver.findElement(By.tagName("p")).getText();

    }

}

This is BasePage page:

package PageObjectPage;


import org.openqa.selenium.WebDriver;

public class BasePage {

    protected WebDriver driver;

    public BasePage (WebDriver driver){
        this.driver = driver;
    }

    public WebDriver getDriver() {
        return this.driver;
    }

    }

This is TestBase page:

package TestBaseSetup;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


public class TestBase {

    WebDriver driver;

    public WebDriver getDriver() {

        return driver;
    }

    @Before
    public void testSetUp(){
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Dragana\\Desktop\\chromedriver.exe ");

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized", "--disable-cache");
        driver = new ChromeDriver(options);

        driver.navigate().to("http://store.demoqa.com/");
    }

    @After
    public void testTearDown(){

        driver.close();
    }

}

This is my Test page:

package test;

import PageObjectPage.HomePage;
import PageObjectPage.LogInResultPage;
import PageObjectPage.MyAccount;
import TestBaseSetup.TestBase;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.WebDriver;


public class AccountTest extends TestBase {

    public WebDriver getDriver() {

        return driver;
    }

    WebDriver driver;

    @Test
    public void shouldLogIn() {

        HomePage onHomePage = PageFactory.initElements(driver, HomePage.class);
        System.out.println("Step 1 ");
        MyAccount onMyAccount = onHomePage.clickOnMyAccount();
        System.out.println("Step 2");
        LogInResultPage onResultPage = onMyAccount.LogIn().submitForm();
        System.out.println("Step 3");
        wait(2000);
        Assert.assertTrue(onResultPage.getMessage().contains("ERROR"));
    }

    public void wait(int seconds){
        try {
            Thread.sleep(2000);
        } catch (final InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Error:

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.$Proxy8.click(Unknown Source)
    at PageObjectPage.HomePage.clickOnMyAccount(HomePage.java:26)
    at test.AccountTest.shouldLogIn(AccountTest.java:31)
    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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
1

There are 1 answers

5
mackowski On BEST ANSWER

1 .Delete:

public WebDriver getDriver() {

    return driver;
}

WebDriver driver;

from AccountTest beacuse it overrides WebDriver from TestBase.

  1. Make WebDriver driver protected in TestBase

protected WebDriver driver;

OR use getDriver() in AccountTest e.g. HomePage onHomePage = PageFactory.initElements(getDriver(), HomePage.class);