I am getting a NullPointerException
while trying to setup a simple AndroidDriver.
The required code is below -
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidElement;
import java.net.URL;
import java.net.MalformedURLException;
import org.openqa.selenium.remote.DesiredCapabilities;
public class TestAutomation {
protected AndroidDriver<MobileElement> driver;
public void setup() throws MalformedURLException, InterruptedException {
URL u = new URL("http://127.0.0.1:4723/wd/hub");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("device", "Android");
capabilities.setCapability("deviceName","Android");
capabilities.setCapability("platformName","Android");
driver = new AndroidDriver<MobileElement>(u, capabilities);
}
public static void main(String[] args) throws Exception {
TestAutomation object = new TestAutomation();
System.out.println(object);
object.setup();
}
}
The Appium Java client version is 6.1.0.
Verified that Appium server is running on localhost at port 4723.
Is there something I am missing?
The Exception I am getting is shown below -
Exception in thread "main" java.lang.NullPointerException at io.appium.java_client.android.AndroidDriver.getCapabilities(AndroidDriver.java:209) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:669) at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42) at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1) at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:249) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:131) at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:144) at io.appium.java_client.DefaultGenericMobileDriver.(DefaultGenericMobileDriver.java:38) at io.appium.java_client.AppiumDriver.(AppiumDriver.java:84) at io.appium.java_client.AppiumDriver.(AppiumDriver.java:94) at io.appium.java_client.android.AndroidDriver.(AndroidDriver.java:93) at TestAutomation.setup(TestAutomation.java:22) at TestAutomation.main(TestAutomation.java:28)
Your have problem with the desired capabilities.
appActivity and appPackage must be included.
device from your desired capabilities must be removed.
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("deviceName", "My Phone");
caps.setCapability("platformName", "Android");
caps.setCapability("appPackage", "your app Package");
caps.setCapability("appActivity",
"your app activity");;
Also update your selenium-java version to latest stable version.