Home » Android » android – Getting Instrumentation run failed due to 'java.lang.NullPointerException'

android – Getting Instrumentation run failed due to 'java.lang.NullPointerException'

Posted by: admin May 14, 2020 Leave a comment


I am trying to write Unit test cases for Activities in my app by extending the test class with ActivityUnitTestCase. I could successfully run the test cases earlier but now I’m always getting the exception while running them. Even though I’m pretty familiar with handling NullPointerExceptions, I couldn’t figure out the problem that’s causing this. I couldn’t find any similar questions so I’m posting this one.

Stack trace shows me there is a null object reference at this line in my code

activity = startActivity(mIntent, null, null);

But the startActivity method is supposed to get the instance of the activity I’m testing. I’m not sure why it’s returning null.

Here is the Stack trace.

java.lang.NullPointerException: Attempt to write to field 'android.os.IBinder android.app.ActivityThread.mLastIntendedActivityToken' on a null object reference
at android.app.Activity.performCreate(Activity.java:6372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:346)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)
at com.abc.test.MainActivityTest.access$100(MainActivityTest.java:16)
at com.abc.test.MainActivityTest$1.run(MainActivityTest.java:34)
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:1891)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Test running failed: Instrumentation run failed due to 'java.lang.NullPointerException'

Here is the Test class

public class MainActivityTest extends ActivityUnitTestCase<MainActivity>{

    private Intent mIntent;
    private MainActivity activity;

    public MainActivityTest() {

    protected void setUp() throws Exception {
        //Create an intent to launch target Activity as it is not automatically started by Android Instrumentation
        mIntent = new Intent(getInstrumentation().getContext(), MainActivity.class);
        //Start the activity under test in isolation, in the main thread to avoid assertion error.
        getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
                activity = startActivity(mIntent, null, null);

    protected void tearDown() throws Exception {

     * Tests the preconditions of this test fixture.
    public void testPreconditions() {
        assertNotNull("MainActivity is null", getActivity());

    public void testSecondActivityWasLaunchedWithIntent() {
        // Get the intent for the next started activity
        final Intent launchIntent = getStartedActivityIntent();
        //Verify the intent was not null.
        assertNotNull("Intent was null", launchIntent);
        //Verify that LaunchActivity was finished
How to&Answers:

@prudhvi I don’t think I have a silver bullet, unfortunately, but I would suggest trying to follow these steps to upgrade to the new test support libraries in the newer versions of the SDK. Sorry that I can’t be of more help!


I had the same issue.
The solution was to make the test an ActivityInstrumentationTestCase2 instead of an ActivityUnitTestCase, and have the activity created for me in the background


There’s a good chance something in your application class (i.e. X extends Application) is crashing early on. If that’s the case you’ll see this error…. Checkout your logcat for traces.


i changed ActivityUnitTestCase to ActivityInstrumentationTestCase2 and removed my startActivity() calls (seems that ActivityInstrumentationTestCase2 automatically starts the activity) now it runs again