java.lang.NullPointerException

Can someone help with this issue please: https://github.com/appium/appium/issues/6580#issuecomment-225718285

As a rudimentary form of debugging, you can add some print statements. In particular, I would add print statements in the following locations:

  1. Before the driver is initialized in the setup method.
  2. After the driver is initialized in the setup method.

Also, this Appium launch parameter looks very suspicious:

--address "127.0.01"

I have rectified that and I still get the same issue.

Also I printed driver and it prints ‘null’.

Can you update and paste what your code looks like? Also, instead of printing the driver itself, try printing a boolean expression testing if the driver actually is null. That is, instead of doing

System.out.println(driver);

do

System.out.println(driver == null);

This should print either true or false to the screen to tell if your your driver was initialized or not. I’m predicting that print statement before the driver is initialized will print true while the print statement after calling the driver’s constructor will print false.

I have updated the print statement to check if it’s null:

[TestNG] Running:
  /Users/mohammedshuheb/Library/Caches/IntelliJIdea2016.1/temp-testng-customsuite.xml
true

Seems like the driver is not initialising.

My class:

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
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.net.URL;

public class FirstTest {

    AppiumDriver driver;

    @Before
    public void setup() throws Exception{
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "AndroidTestDevice");

        driver = new AndroidDriver(new URL("127.0.0.1:4723/wd/hub"), capabilities);
    }

    @Test
    public void testOne() {
        System.out.println(driver == null);
    }

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

Try putting print statements around your actual constructor call like this:

System.out.println(driver == null);
driver = new AndroidDriver(new URL("127.0.0.1:4723/wd/hub"), capabilities);
System.out.println(driver == null);

What I’m suspecting is that the AndroidDriver constructor is throwing an exception, and I don’t recall JUnit tests recording exceptions in its logs when the exception occurs in a configuration method. If you only see 1 statement printed above (and the printed statement should be “true”). then this means the AndroidDriver is throwing an exception. If that’s the case, you might want to print the exception and its stacktrace to see what’s going on:

try {
    driver = new AndroidDriver(blah blah blah);
} catch(Exception e) {
    System.out.println(e);
}

Tried wrapping the constructor with print statements and prints true.

Also, tried the try catch, however, it only runs the test.

Then this means that an Exception is being thrown. What gets printed for the Exception?

I added a couple of breakpoints for debugging and figured that the @Before tag isn’t being run for some reason.

I then added @BeforeTest - this got rid of the null business, however, now it says java.net.MalformedURLException: no protocol: 127.0.0.1:4723/wd/hub.

So I’ve imported:

import java.net.MalformedURLException

but still no luck.

Ah, I see now. It looks like you were running this as a TestNG test rather than a JUnit test. That would definitely cause issue with driver not being set up.

“No protocol” means exactly what it says. It’s missing the “http://” protocol bit in your URL. Instead of saying 127.0.0.1:4723/wd/hub, you should say http://127.0.0.1:4723/wd/hub.

Why can’t I run TestNG tests? I am in IntelliJ it comes with TestNG libraries? Am I right?

Also, now that I have the code working, I keep getting some sort of ADB error in the Appium logs:

`[HTTP] --> POST /wd/hub/session {“desiredCapabilities”:{“platformName”:“Android”,“deviceName”:“AndroidTestDevice”}}
[MJSONWP] Calling AppiumDriver.createSession() with args: [{“platformName”:“Android”,…
[Appium] Creating new AndroidDriver session
[Appium] Capabilities:
[Appium] platformName: ‘Android’
[Appium] deviceName: ‘AndroidTestDevice’
[Appium] platformVersion: ‘5.1’
[Appium] automationName: ‘Appium’
[Appium] app: ‘/Users/mohammedshuheb/Downloads/Cocoon_1.0.1112.apk’
[BaseDriver] Session created with session id: c68e462d-5ea1-4e5c-8bab-b5d84d7be2b9
[debug] [AndroidDriver] Getting Java version

[AndroidDriver] Java version is: 1.8.0_91

[ADB] Checking whether adb is present

[ADB] Using adb from /Users/mohammedshuheb/Library/Android/sdk/platform-tools/adb

[AndroidDriver] Retrieving device list
[debug] [ADB] Trying to find a connected android device
[debug] [ADB] Getting connected devices…

[debug] [ADB] 1 device(s) connected

[AndroidDriver] Looking for a device with Android 5.1

[debug] [ADB] Setting device id to CB5A27SHRZ
[ADB] Getting device platform version
[debug] [ADB] Getting connected devices…

[debug] [ADB] 1 device(s) connected

[debug] [ADB] Running /Users/mohammedshuheb/Library/Android/sdk/platform-tools/adb with args: ["-P",5037,"-s",“CB5A27SHRZ”,“shell”,“getprop”,“ro.build.version.release”]

[AndroidDriver] Error: Unable to find an active device or emulator with OS 5.1. The following are available: CB5A27SHRZ (6.0)
at Object.wrappedLogger.errorAndThrow (lib/logger.js:60:13)
at Object.callee$0$0$ (lib/android-helpers.js:154:16)
at tryCatch (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at run (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/node_modules/core-js/library/modules/es6.promise.js:104:47)
at /Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/node_modules/core-js/library/modules/es6.promise.js:115:28
at flush (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/node_modules/core-js/library/modules/$.microtask.js:19:5)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
[Error: Unable to find an active device or emulator with OS 5.1. The following are available: CB5A27SHRZ (6.0)]

[debug] [AndroidDriver] Shutting down Android driver

[MJSONWP] Encountered internal error running command: Error: Unable to find an active device or emulator with OS 5.1. The following are available: CB5A27SHRZ (6.0)
at Object.wrappedLogger.errorAndThrow (lib/logger.js:60:13)
at Object.callee$0$0$ (lib/android-helpers.js:154:16)
at tryCatch (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)
at GeneratorFunctionPrototype.invoke [as _invoke] (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)
at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)
at GeneratorFunctionPrototype.invoke (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)
at run (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/node_modules/core-js/library/modules/es6.promise.js:104:47)
at /Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/node_modules/core-js/library/modules/es6.promise.js:115:28
at flush (/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/babel-runtime/node_modules/core-js/library/modules/$.microtask.js:19:5)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)

[AndroidDriver] Cannot shut down Android driver; it has already shut down
[HTTP] <-- POST /wd/hub/session 500 260 ms - 239

[HTTP] --> GET /wd/hub/status {}

[MJSONWP] Calling AppiumDriver.getStatus() with args: []

[MJSONWP] Responding to client with driver.getStatus() result: {“build”:{“version”:“1.5.3”…
[HTTP] <-- GET /wd/hub/status 200 11 ms - 83 `

Your import statements look like this:

The Appium logs show your error:

Appium is looking for an Android device running 5.1 because your Appium launch command (copied from your Github post that included the launch command) is set to run on 5.1 devices if the desired capabilites passed in through your test do not specify the version:

I recommend removing that parameter from your launch command. If you’re using the GUI, you can also remove it from the GUI. This will let Appium choose whatever Android device is connected to ADB for automation.