0

I am learning appium and trying to call an object from one class to another and facing null pointer exception. Below is my code :

public class TestCommons {

    public AndroidDriver driver;

    public void setUp() {

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "MotoE");
        File file = new File("D:/APK1/com.vector.guru99.apk");
        capabilities.setCapability("app", file);

        try {
            driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public void tearDown() {

        driver.closeApp();
    }
}

I wanted to use above class i.e "TestCommons" in other class. I want to use driver object.

Second class is below :

public class Day03 extends TestCommons {

    TestCommons commons = new TestCommons();

    @BeforeClass
    public void beforeClass() {
        commons.setUp();

    }

    @Test(enabled = true)
    public void f() {

        if (driver.findElement(By.id("com.vector.guru99:id/action_quiz")).isDisplayed()) {
            System.out.println("Quiz is displayed");
            driver.findElement(By.id("com.vector.guru99:id/action_quiz")).click();
            System.out.println("quiz is click");

        }
    }

    @AfterClass(enabled = true)
    public void afterClass() {
        commons.tearDown();
    }

}

Getting null pointer in second program @:

if(driver.findElement(By.id("com.vector.guru99:id/action_quiz")).isDisplayed();

Can anyone clarify me please.

Strider
  • 4,452
  • 3
  • 24
  • 35
naazneen3264
  • 315
  • 2
  • 7
  • 22

2 Answers2

0

You have one of two issues.

1) driver was not set properly in setUp(). If this is the case you probably got an exception. Check your logs to make sure that there isn't an exception there.

2) driver.findElement(By.id("com.vector.guru99:id/action_quiz")) is returning null. You can check this by setting a debug point and running evaluate expression on that call.

Chris Wilson
  • 248
  • 1
  • 9
0

try this way:

public class TestCommons {

    public static AndroidDriver driver;
    @BeforeClass
    public void setUp() {

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "MotoE");
        File file = new File("D:/APK1/com.vector.guru99.apk");
        capabilities.setCapability("app", file);

        try {
            driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    @AfterClass
    public void tearDown() {

        driver.closeApp();
    }
}

public class Day03 extends TestCommons {

       @Test(enabled = true)
    public void f() {

        if (driver.findElement(By.id("com.vector.guru99:id/action_quiz")).isDisplayed()) {
            System.out.println("Quiz is displayed");
            driver.findElement(By.id("com.vector.guru99:id/action_quiz")).click();
            System.out.println("quiz is click");

        }
    }



}
  • Thanks ! its working perfectly. Could you please explain me the following points : 1. we declared "driver" as static , so no need to instantiate.I understood the concept 2. We are initialising in void setUp() as "new=....". Are we overriding the driver ? – naazneen3264 Mar 01 '16 at 08:21