Null pointer issue while running tests

Hi,

I am not able to appium tests using testng suite by invoking my mobile application in genymotion.
When I run my tests, my app is invoked in genymotion but unable to click the element.

public class Example {

@BeforeTest
public void openBrowser() throws MalformedURLException, InterruptedException
{
AndroidDriver driver = new AndroidDriver(new URL(“http://127.0.0.1:4723/wd/hub”),capabilities);
System.out.println(driver);
}
@Test
public void cases() throws Exception {
driver.findElementByAccessibilityId(“UPDATE”).click();
}
}
I get this as an output:
Android: null
FAILED: cases
java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787)
at org.openqa.selenium.support.ui.FluentWait.(FluentWait.java:96)
at org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:71)
at org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:45)
at com.javacodegeeks.testng.maven.TestNgMavenExample.cases(TestNgMavenExample.java:115)
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(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:348)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:343)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:305)
at org.testng.SuiteRunner.run(SuiteRunner.java:254)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0

===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0

So, I checked the status of appium URL: http://127.0.0.1:4723/wd/hub/status
my response: 200 OK
{
“status”: 0,
“value”: {
“build”: {
“version”: “1.12.1”
}
},
“sessionId”: null
}
I couldn’t understand where am i going wrong. Please help me in resolving it

Thanks in advance

I did more or less same with Real Device, and it works for me. Can you check your code where you are setting capabilities.

Cheers,
Vishnu

Thanks for the info :grinning:
I have tired it both real and emulator devices but still facing the same issue…couldn’t able to figure it out
These are the capabilities:
DesiredCapabilities capabilities = new DesiredCapabilities();
DefaultExecutor executor = new DefaultExecutor();
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
capabilities.setCapability(“deviceName”,“192.168.27.101:5555”);
capabilities.setCapability(“platformVersion”, “8.0”);
capabilities.setCapability(“platformName”, “Android”);
capabilities.setCapability(“app”,“E:\Aparna\apk\example.apk”);
capabilities.setCapability(“appPackage”, “io.ionic.starter”);
capabilities.setCapability(“appActivity”, “io.ionic.starter.MainActivity”);
capabilities.setCapability(“newCommandTimeout”, “12000”);

thanks again

Try with my working code below:

//Set the Desired Capabilities
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(“deviceName”, “My Phone”);
caps.setCapability(“udid”, “ZW22233J57”);
caps.setCapability(“platformName”, “Android”);
caps.setCapability(“platformVersion”, “7.0”);
caps.setCapability(“appPackage”, “net.XXXXX”);
caps.setCapability(“appActivity”, “net.XXXXX”);
caps.setCapability(“noReset”, “true”);

	//Instantiate Appium Driver
	try 
	{
		driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
	}
	catch (MalformedURLException e)
	{
		System.out.println("MalformedURLException Caught: "+e.getMessage());
		e.printStackTrace();
	}

WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“userNameInput”)));

		WebElement username = driver.findElementById("userNameInput");
		username.click();
		username.sendKeys("XXXXXX");

Android driver object is declared inside a method , try declaring it at class level and initialize it in openBrowser.
Also do not use throws Exception unnecessarily, resolve the errors instead of throwing directly.