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.