0

I am new to Appium and just getting started, I have been following an example and using a basic Contacts apk on Android tablet to start against. The code I have is pretty much copied from the example I am following but when I try to run the test I get a null pointer exception. I did some debugging and find that the driver = null is why I am getting this exception. I looked around and found some code that I thought might help but it hasn't.

The code I have is

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import java.net.MalformedURLException;
import java.net.URL;

public class addContact {

    AppiumDriver driver;

    @Before
    public void setUp () throws Exception {

        new DesiredCapabilities();
        DesiredCapabilities capabilities = DesiredCapabilities.android();
           capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"AndroidTestDevice");

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

        }
        catch (MalformedURLException e)
        {
            System.out.println("URL init error");
        }

    }

    @After
    public void tearDown () throws Exception {
        driver.quit();
    }

    @Test
    public void addNewContact (){


     System.out.println (driver);
     WebElement addContactButton = driver.findElementById("com.example.android.contactmanager:id/addContactButton");
     addContactButton.click();
    }

}`

The exception I get is :

java.lang.NullPointerException
at addContact.addNewContact(addContact.java:49)

and the line this occurs in is :

WebElement addContactButton = driver.findElementById("com.example.android.contactmanager:id/addContactButton");
4
  • Is your setUp correctly called, and doesn't give a "URL init error" ? Commented Dec 22, 2015 at 15:27
  • Impossible to say why it's null from this code... There are many possibilities: setUp() isn't called (or is called pmöy after calling addNewContact(), not before), an Exception occured in the AppiumDriver constructor, etc.... Commented Dec 22, 2015 at 15:27
  • I thought setUp was always called first since its tagged @Before? Is that not the case? If not, that would certainly explain the problem I have but how do I force the setUp to execute first? Commented Dec 22, 2015 at 15:42
  • Possible duplicate of Null Pointer Exception Being returned - Java Selenium Webdriver Commented Sep 10, 2019 at 5:12

1 Answer 1

0

The reason it showing as null pointer because you are using "testng" annotation for "Tests" and "junit" annotation for "Before and after". Change import org.testng.annotations.Test; to import org.junit.Test; and run it as junit test.

This should work, just tested it on my side.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhhh... Thanks so much Gaurav!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.