Search code examples
androidappiumappium-android

java.lang.NullPointerException when running a testcase via Selenium Android driver


I am getting 'Null pointer error' while executing script. But app is getting launched properly on Geny motion but fails to perform further action like clicking on 'Skip user' button. Below is the code I have written for it

public class mainProgram {
    public static AndroidDriver driver;

  @Test
  public void skipUser() { 
  driver.findElement(By.id("com.hp.pregnancy.lite:id/btn_join_later")).click();
  }

  @BeforeMethod
  public void beforeMethod() {
      try {
            File app = new File("D:\\Automation builds\\Testbuild.apk");   
            DesiredCapabilities caps = new DesiredCapabilities();     
            caps.setCapability("deviceName", "Google Nexus 6P - 7.1.0 - API 25 - 1440x2560");
            caps.setCapability("udid", "192.168.250.101:5555"); //Give Device ID of your mobile phone
            caps.setCapability("platformName", "android");
            caps.setCapability("platformVersion", "7.1.0");
            caps.setCapability("appPackage", "com.hp.pregnancy.lite");
            caps.setCapability("appActivity", "com.hp.pregnancy.lite.onboarding.SplashScreenActivity");
            caps.setCapability("noReset", "true");
            caps.setCapability("autoAcceptAlerts", "true");
            caps.setCapability("autoDismissAlerts", "true");
        //  caps.setCapability("fullReset",false);
            caps.setCapability("app", app.getAbsolutePath()); 

        Androiddriver driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);

            System.out.println("Pregnancy+ application launched successfully on Genymotion");
            Thread.sleep(5000);
            } catch (Exception e) {
                System.out.println("Step failed - Unable to setup with the predefined Capabilities");
            }
  }

  @AfterMethod
  public void afterMethod() {
      System.out.println("afterMethod");
  }
}

Solution

  • You have used two drivers in your code. One is at the global level which you have declared as public static and one you have used and initialised in your @BeforeMethod.
    So, as the driver inside the @BeforeMethod is initialised by using AndroidDriver driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps); it is working fine and opening the app. But after that, your code goes to the @Test where the driver you are using is the global one and that driver is not initialised and that's why you are getting NullPointerException

    To solve this problem, please use the same AndroidDriver driver in both the @BeforeMethod and @Test

    So, you just need to use driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps); (which would be initialising the global driver and would be used in your @Test) in your @BeforeMethod instead of making another AndroidDriver driver there, like you were doing by using AndroidDriver driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);