Null Pointer Exception while Using PageFactory Design Pattern

I am trying to use PageFactoty Design Pattern but getting Null Pointer Exception.
Please suggest any improvement required.

public DriverCapabilities() throws IOException{

	URL url;
	try {

		DesiredCapabilities caps = new DesiredCapabilities();
		caps.setCapability("newCommandTimeout", "90");
		caps.setCapability("browserName", "");

			caps.setCapability("appActivity",Config.appActivity );
			caps.setCapability("appPackage", Config.appPackage);
			caps.setCapability("platformName", "Android");
			caps.setCapability("platformVersion", "5.0");
			caps.setCapability("deviceName", "Android Emulator");

		caps.setCapability("app", Config.path);

		caps.setCapability("appium-version", "");
		url = new URL("");


		innerDriver = Config.os.equalsIgnoreCase("ANDROID")? new AndroidDriver(url, caps): new IOSDriver(url, caps);
		PageFactory.initElements(new AppiumFieldDecorator(innerDriver, 5, TimeUnit.SECONDS), this);

Sample Screen Class:
public class DemoScreen{
@AndroidFindBy(id = “eula_accept”)
private static RemoteWebElement eula;

public static void submit() throws IOException{

	 init.getInstance(); //To initialize the driver
public static WebElement getEula() {
	return eula;

Sample Test code

public class Demo1{
public void test1() throws IOException {

I’m getting below error:

at automation.screens.DemoScreen.submit(
at automation.scripts.Demo1.test1(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(
at org.testng.internal.Invoker.invokeMethod(
at org.testng.internal.Invoker.invokeTestMethod(
at org.testng.internal.Invoker.invokeTestMethods(
at org.testng.internal.TestMethodWorker.invokeTestMethods(
at org.testng.TestRunner.privateRun(
at org.testng.SuiteRunner.runTest(
at org.testng.SuiteRunner.runSequentially(
at org.testng.SuiteRunner.privateRun(
at org.testng.SuiteRunnerWorker.runSuite(
at org.testng.TestNG.runSuitesSequentially(
at org.testng.TestNG.runSuitesLocally(
at org.testng.remote.RemoteTestNG.initAndRun(
at org.testng.remote.RemoteTestNG.main(

I have already tried with RemoteWebElement, WebElement and MobileElement for PageFactory.initElements

Try AjaxElementLocatorFactory factory = new AjaxElementLocatorFactory(this.driver,30);

driver = android driver

Thanks for the prompt reply.
I tried your workaround but got the same error

  1. create abstract Page:

public abstract class Page {
public Page(WebDriver driver) {
this.driver = driver;

  1. update your Page:

public class DemoScreen extends Page{

@AndroidFindBy(id = “eula_accept”)
private WebElement eula;

public DemoScreen(WebDriver driver) {
PageFactory.initElements(new AppiumFieldDecorator(driver,Integer.parseInt(10,TimeUnit.SECONDS), this);

public void submit() {
public WebElement getEula() {
return eula;

  1. do your test like:

public class Demo1{
public void test1() throws IOException {
DemoScreen demoscreen;
demoscreen = new DemoScreen(driver);

  1. add Before test e.g. here to start driver:

public void beforeMethod() throws Exception {
// fill here your capatibilities
driver = new AndroidDriver(new URL(“”), capabilities);

In Page abstract class we need not have to declare WebDriver instance? inside constructor u asked to call this.driver = driver

yes. add “protected WebDriver driver;”. just fogotten to add

1 Like

Hey Thanks… It worked.
But why it is necessary to add abstract class and instantiate driver object there?
Also, AppiumDriver functions are not exposed by using WebDriver. Like swipe(),scroll,TouchActions.
Instead all WebDriver functions are exposed. I tried passing AppiumDriver instead of WebDriver, code works well but still WebDriver specific in-built functions are accessible not AppiumDriver

  1. Abstract just enhance functionality. you may add many code here instead of each page.
  2. to use specific driver do: ((IOSDriver) driver). or ((AndroidDriver) driver.

Yes… still appium specific methods are not getting exposed

write code and what you are expecting, Please find above project with PageFactory implementation with Appium.

Thanks… It did helped

Getting same exception even after creating abstract class

I am using
java-client - 5.0.4
Appium - 1.7.2

Abstract class -

public abstract class object {
public static final String appPkgName = “com.test.test”;
public AndroidDriver<?> driver;

public object(AndroidDriver<?> driver) {
	this.driver = driver;
public void loadPage(){
	PageFactory.initElements(new AppiumFieldDecorator(driver), this);		


public class LoginPage extends object{
	private static final Logger log = Logger.getLogger(LoginPage.class.getName());
	WebElement BtnID;
	@AndroidFindBy(id = appPkgName + ":id/login_username")
	WebElement inputUserName;
	@AndroidFindBy(id= appPkgName + "id/logikn_password")
	WebElement inputPassword;
	@AndroidFindBy(xpath="//android.widget.Button[@text='Sign In']")
	WebElement BtnSignIn;

	public LoginPage(AndroidDriver driver ){
	public void login(String email,String pass) throws InterruptedException {


public class Login extends TestBase{
	private static final Logger log = Logger.getLogger(Login.class);
	public static AndroidDriver driver;
	static LoginPage login;
	public static void main(String[] args) throws IOException, InterruptedException {
		loadCapFromProperties();"Application launch succesfully");
		login = new LoginPage(driver);
		login.login(USER_NAME, PASSWORD);"Login succesfully");

Following exception occur while serching for the element

log4j:WARN Failed to set property [append] to value "true;". 
2018-02-18 10:31:24 INFO  TestBase:57 - using DEVICE_NAME{LS5510}
2018-02-18 10:31:24 INFO  TestBase:61 - using PLATFORM_NAME{Android}
2018-02-18 10:31:24 INFO  TestBase:64 - using APP_PACKAGE{com.test.test}
2018-02-18 10:31:24 INFO  TestBase:66 - using APP_ACTIVITY{}
Feb 18, 2018 10:31:45 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
2018-02-18 10:31:45 INFO  Login:19 - Application launch succesfully
log4j:WARN Failed to set property [append] to value "true;". 
Exception in thread "main" org.openqa.selenium.WebDriverException: java.lang.NullPointerException
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T15:28:36.4Z'
System info: host: 'WINCTRL-6JIFFF1', ip: '', 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_144'
Driver info: driver.version: unknown
	at io.appium.java_client.pagefactory.AppiumElementLocator$WaitingFunction.apply(
	at io.appium.java_client.pagefactory.AppiumElementLocator$WaitingFunction.apply(
	at io.appium.java_client.pagefactory.AppiumElementLocator.waitFor(
	at io.appium.java_client.pagefactory.AppiumElementLocator.findElement(
	at io.appium.java_client.pagefactory.interceptors.InterceptorOfASingleElement.intercept(
	at org.openqa.selenium.remote.RemoteWebElement$$EnhancerByCGLIB$$d27c0df4.getText(<generated>)
	at pages.LoginPage.login(
	at testCases.Login.main(

Caused by: java.lang.NullPointerException
at io.appium.java_client.pagefactory.AppiumElementLocator.lambda$0(
at io.appium.java_client.pagefactory.AppiumElementLocator$WaitingFunction.apply(
… 8 more