java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208)

I am getting NullPointerException in POM for Appium. I have tried to debug but unable to locate the root cause. Can please assist?

I am able to launch the Appium Server and app but it fails at init with below error message. I have shared the structure of my code, kindly assist.

java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:208)
at org.openqa.selenium.support.ui.FluentWait.(FluentWait.java:106)
at org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:85)
at org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:45)
at base.ScreenBase.(ScreenBase.java:15)
at screens.LoginScreen.(LoginScreen.java:70)
at testCases.LoginWithPassword.init(LoginWithPassword.java:26)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:217)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:144)
at org.testng.TestRunner.beforeRun(TestRunner.java:634)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

ScreenBase

package base;

import org.openqa.selenium.support.ui.WebDriverWait;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;

public class ScreenBase{

protected AppiumDriver<MobileElement> driver;
public WebDriverWait wait = new WebDriverWait(driver, 180);

public ScreenBase(AppiumDriver<MobileElement> driver) {
	
	this.driver=driver;
}

public void hideKeyboard() {
	
	driver.hideKeyboard();
}

public void enter() {
	
	((AndroidDriver) driver).pressKey(new KeyEvent(AndroidKey.ENTER));
}

}

BeforeLogin

package screens;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import base.ScreenBase;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;

public class BeforeLoginMore extends ScreenBase{

@FindBy(id="el1")
public WebElement MoreButton;

@FindBy(id="el2")
public WebElement AppVersion;

@FindBy(xpath="el3")
public WebElement CloseBeforeLoginMore;


public BeforeLoginMore(AppiumDriver<MobileElement> driver){
	super(driver);
	PageFactory.initElements(driver, this);

}

//methods

public void checkVersion() throws InterruptedException{
	
	
	MoreButton.click();
	String appVersion = AppVersion.getText();
	System.out.println("App Version is : " + appVersion);
	CloseBeforeLoginMore.click();
	
	Thread.sleep(5000);
	
}

}

Login Screen

package screens;
import java.util.List;

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;

import com.aventstack.extentreports.Status;

import base.ScreenBase;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;

public class LoginScreen extends ScreenBase{

@FindBy(id="el1")
public WebElement yesLogo;

@FindBy(id="el2")
public WebElement enterYesID;

@FindBy(id="el3")
public WebElement enterPassword;

@FindBy(id="el4")
public WebElement moreButton;

@FindBy(xpath="el5")
public WebElement adminPanelUserID;

@FindBy(xpath="el6")
public WebElement adminPanelPwd;

@FindBy(xpath="el7")
public WebElement adminPanelLogin;

@FindBy(xpath="el8")
public WebElement chooseEnv;

@FindBy(xpath="el9")
public WebElement selectIOT;

@FindBy(xpath="el10")
public WebElement selectApply;

@FindBy(xpath="el11")
public WebElement selectYes;

@FindBy(xpath="el12")
public WebElement selectOK;

@FindBy(id="el13")
public WebElement LoginYesID;

@FindBy(id="el14")
public WebElement PasswordButton;

@FindBy(xpath="el15")
public WebElement PasswordField;

@FindBy(id="el16")
public WebElement Login;

@FindBy(xpath="el17")
public WebElement CoachmarkCloseButton;


public LoginScreen(AppiumDriver<MobileElement> driver){
	super(driver);
	PageFactory.initElements(driver, this);
	
}


// methods

public void changeEnv() throws InterruptedException{
	
	wait.until(ExpectedConditions.visibilityOf(yesLogo));
	Actions action = new Actions(driver).moveToElement(yesLogo);
	wait.until(ExpectedConditions.elementToBeClickable(yesLogo));
	for(int i = 0; i < 11; i++) {
		action.sendKeys(Keys.ENTER).perform();
	}
	
	adminPanelUserID.sendKeys("xxx");
	adminPanelPwd.sendKeys("xxxx");
	adminPanelLogin.click();
	driver.findElement(MobileBy.AndroidUIAutomator(
	        "new UiScrollable(new UiSelector().scrollable(true)).setMaxSearchSwipes(10)" +
	         ".scrollIntoView(new UiSelector().text(\"Select Server\"))")).getText();
	chooseEnv.click();
	selectIOT.click();
	selectApply.click();
	selectYes.click();
	selectOK.click();

	Thread.sleep(5000);
	//return new LoginScreen(driver);
	
}

public void loginWithPwd() throws InterruptedException{
	
	LoginYesID.sendKeys("xxxx");
	PasswordButton.click();
	PasswordField.sendKeys("xxxxx");
	Login.click();
	Thread.sleep(30000);
	CoachmarkCloseButton.click();
	
	
	Thread.sleep(5000);
	
}

}

TestBase

package base;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import java.util.Objects;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import utilities.AppiumServer;
import utilities.CommonUtils;

public class TestBase{

public AppiumDriver<MobileElement> driver;
public static String loadPropertyFile = "Android_YesApp.properties";
public static Logger log = Logger.getLogger(TestBase.class);

public void setUp() {
	
	if (driver == null) {

		PropertyConfigurator
				.configure(System.getProperty("user.dir") + "\\src\\test\\resources\\properties\\log4j.properties");

		if (loadPropertyFile.startsWith("Android_")) {

			AppiumServer.start();
			log.info("Appium server started");
			CommonUtils.loadAndriodConfProp(loadPropertyFile);
			log.info(loadPropertyFile + " properties file loaded !!!");
			CommonUtils.setAndroidCapabilities();
			driver = CommonUtils.getAndroidDriver();

		} else if (loadPropertyFile.startsWith("IOS_")) {

		}

	}
	
}

public void tearDown(){
	
	try {
		Thread.sleep(3000);
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

	driver.quit();
	log.info("Test case execution completed");

	AppiumServer.stop();
	log.info("Appium server stopped!!!");

}

}

Login with Password Test Case

package testCases;

import java.util.concurrent.TimeUnit;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import base.TestBase;
import screens.BeforeLoginMore;
import screens.LoginScreen;

public class LoginWithPassword extends TestBase {

LoginScreen loginScreen;
BeforeLoginMore beforeLoginMoreScreen;



@BeforeClass
public void init(){
	
	setUp();
	loginScreen = new LoginScreen(driver);
	beforeLoginMoreScreen = new BeforeLoginMore(driver);
	
	
}

@Test(priority=1)

public void changeEnv(){
	
	try {
		loginScreen.changeEnv();
	}catch (Exception e) {
        log.error("Exception occured in while changing environment", e);
    }
	try {
		beforeLoginMoreScreen.checkVersion();
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

}

@Test(priority=2)
public void logintoAcct(){
	
	try {
		loginScreen.loginWithPwd();
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}

@AfterClass
public void quitDriver() {
	
	tearDown();
}

}

driver is obviously null at the time this initializer is called.

1 Like

Thank you so much! Somehow I missed this. It is working fine now after I declared it in the screen class itself.