user5546140
user5546140

Reputation: 21

Android - Test run failed - NullPointerException

I just set up my first JUnit Test Case Project for the Android application I'm developing. I've added a (very simple) Test Case for one of the projects classes, but can't seem to get it to run. I've followed a couple of separate tutorials just to be sure I wasn't overlooking something when writing the test case.

I'm getting a NullPointerException - Attempt to invoke virtual method 'java.lang.Classloader android.content.Context.getClassLoader() on a null object reference. I've done multiple searches looking for the same issue, but haven't been able to find a solution. Below is the logcat output (note: I've replaced my package names with generic terms before posting):

11-10 09:56:42.942: E/AndroidRuntime(31147): FATAL EXCEPTION: main
11-10 09:56:42.942: E/AndroidRuntime(31147): Process: <target package name>, PID: 31147
11-10 09:56:42.942: E/AndroidRuntime(31147): java.lang.RuntimeException: Exception thrown in onCreate() of ComponentInfo{<test package name>/android.test.InstrumentationTestRunner}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.ClassLoader android.content.Context.getClassLoader()' on a null object reference
11-10 09:56:42.942: E/AndroidRuntime(31147):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5107)
11-10 09:56:42.942: E/AndroidRuntime(31147):    at android.app.ActivityThread.access$1600(ActivityThread.java:177)
11-10 09:56:42.942: E/AndroidRuntime(31147):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1509)
11-10 09:56:42.942: E/AndroidRuntime(31147):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-10 09:56:42.942: E/AndroidRuntime(31147):    at android.os.Looper.loop(Looper.java:145)
11-10 09:56:42.942: E/AndroidRuntime(31147):    at android.app.ActivityThread.main(ActivityThread.java:5942)
11-10 09:56:42.942: E/AndroidRuntime(31147):    at java.lang.reflect.Method.invoke(Native Method)
11-10 09:56:42.942: E/AndroidRuntime(31147):    at java.lang.reflect.Method.invoke(Method.java:372)
11-10 09:56:42.942: E/AndroidRuntime(31147):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
11-10 09:56:42.942: E/AndroidRuntime(31147):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
11-10 09:56:42.942: E/AndroidRuntime(31147): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.ClassLoader android.content.Context.getClassLoader()' on a null object reference
11-10 09:56:42.942: E/AndroidRuntime(31147):    at android.test.AndroidTestRunner.loadTestClass(AndroidTestRunner.java:87)
11-10 09:56:42.942: E/AndroidRuntime(31147):    at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:50)
11-10 09:56:42.942: E/AndroidRuntime(31147):    at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:379)
11-10 09:56:42.942: E/AndroidRuntime(31147):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5104)
11-10 09:56:42.942: E/AndroidRuntime(31147):    ... 9 more

Upvotes: 1

Views: 2787

Answers (2)

user5546140
user5546140

Reputation: 21

Turns out (as suspected) I overlooked something fairly obvious. I am actually extending an Instrumentation Test class, and though I had an Android device connected, I did not have the activity to be tested running.

Ran my app first, then the test case, and it worked fine.

Upvotes: 1

MattMatt
MattMatt

Reputation: 2310

That sounds config/setup related (assuming you're using Android Studio here):

  • Ensure that you have placed the tests in the correct location if you're using Android Studio/Gradle command line to run your tests.
  • Ensure that you have selected the correct Test Artifact in the Build Variants section of Android Studio; you need to select Android Instrumentation Tests if you're using Instrumentation-dependent tests, and Unit Tests if you're using pure JUnit tests that have no Instrumentation dependencies.

As you're getting a NPE on InstrumentationTestRunner, I reckon you've got Unit Test selected and that's causing the problem.

It's described in this section of the Vogella tutorial on testing in more detail.

Upvotes: 1

Related Questions