0

I am trying to test the login function to my android app. But it displays an exception error java.lang.NullPointerException.

Code as follows:

package tests;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Amazon
{
 public static AppiumDriver<MobileElement> driver;
 @BeforeTest
 public void launch() throws IOException
 {
 // Set the Desired Capabilities
 DesiredCapabilities caps = new DesiredCapabilities();
 caps.setCapability("deviceName", "Bright Roots (Galaxy S4)");
 caps.setCapability("udid", "4d00b778d48a421b"); // Give Device ID of your mobile phone
 caps.setCapability("platformName", "Android");
 caps.setCapability("platformVersion", "5.0.1");
 caps.setCapability("appPackage", "com.example.babilok");
 caps.setCapability("appActivity", "com.example.babilok.MainActivity");
 caps.setCapability("noReset", "true");
 try {
 AppiumDriver<MobileElement> Driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"),
 caps);
 } catch (MalformedURLException e) {
 System.out.println(e.getMessage());
 }
 }
 @Test
 public void print() throws Exception
 {
 // locate the Text on the calculator by using By.index()
 MobileElement driver = driver.find_element_by_xpath("//android.widget.Button[@text='7']")
 driver.click();
 }
// @AfterTest
//
// public void close() throws IOException
//
// {
// driver.quit();
//
// }
}

Error:

[RemoteTestNG] detected TestNG version 7.0.0 Oct 19, 2019 3:48:46 PM io.appium.java_client.remote.AppiumCommandExecutor1ドル lambda0ドル INFO: Detected dialect: W3C FAILED: print java.lang.NullPointerException at tests.Amazon.print(Amazon.java:58) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133) at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584) at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172) at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46) at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804) at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128) at java.util.ArrayList.forEach(Unknown Source) at org.testng.TestRunner.privateRun(TestRunner.java:770) at org.testng.TestRunner.run(TestRunner.java:591) at org.testng.SuiteRunner.runTest(SuiteRunner.java:402) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355) at org.testng.SuiteRunner.run(SuiteRunner.java:304) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180) at org.testng.TestNG.runSuitesLocally(TestNG.java:1102) at org.testng.TestNG.runSuites(TestNG.java:1032) at org.testng.TestNG.run(TestNG.java:1000) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

Niels van Reijmersdal
32.7k4 gold badges59 silver badges125 bronze badges
asked Oct 19, 2019 at 10:58
2
  • I don't really know Java that much, but let's see if this will be helpful. Are you sure this line MobileElement driver = driver.find_element_by_xpath("//android.widget.Button[@text='7']") is correct? Is driver different from NULL here? You're also creating an object Driver, shouldn't you use this one? I mean Java is case-sensitive. Commented Oct 19, 2019 at 15:21
  • Can you please format the code and the post? Commented Oct 20, 2019 at 10:53

1 Answer 1

3

In the test you use a property driver which has not been instantiated, because you create the driver into local a variable called Driver. See the case-difference? The class property driver has a default value of NULL and that is why you get a nullpointer exception when you try to use it. You cannot use objects that are NULL.

This is a foundational Java programming language issue, I suggest you first learn the programming language before you start automating tests in it. This book helped me a lot to understand Java and how to use its classes and standard libraries better. I really suggest you learn how to use Java and its basic OO-concepts first before you continue with your trail and error.

Code change I think might make your example work. This code:

 AppiumDriver<MobileElement> Driver = new AndroidDriver<MobileElement>(new URL("http://0.0.0.0:4723/wd/hub"),
 caps);

Should be:

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

Because you want to use the class variable driver and not create a new local variable Driver.

This code:

 MobileElement driver = driver.find_element_by_xpath("//android.widget.Button[@text='7']")
 driver.click();

Should be:

 MobileElement element = driver.find_element_by_xpath("//android.widget.Button[@text='7']")
 element.click();

You cannot re-assing the driver, you want a new variable to interact with elements.

answered Oct 21, 2019 at 8:02
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.