We have a JUnit test class which extends ActivityInstrumentationTestCase2<CommentActivity>
. The test (and the class we're testing) use CommentContentProvider
, which extends ContentProvider
, to access the SQLite database, and we're getting a NullPointerException
[full stack trace below] when running a query on the provider.
We instantiate a MockContentResolver as shown:
MockContentResolver mResolver;
public void setUp() {
super.setUp();
CommentContentProvider ccp = new CommentContentProvider();
mResolver = new MockContentResolver();
mResolver.addProvider(CommentContentProvider.AUTHORITY, ccp);
}
Later on, in our tests, when calling the following code, we get a NullPointerException
:
Cursor mCursor = mResolver.query(Uri.parse(mUri), null, null, null, null);
We get the same result even if we wait to instantiate MockContentResolver
until we have a copy of the activity under test:
mActivity = getActivity();
MockContentResolver mResolver = new MockContentResolver(mActivity);
We have verified that mActivity
is not null.
A colleague stepped through the Android source (not installed on our system) and found that the proximate cause of the error is that getContext()
returns null on the first line of ContentProvider.enforceReadPermissionInner().
We took a look at this question which originally seemed similar, but I think it was a different problem entirely.
This question is also a similar symptom of a problem, but they didn't instantiate their MockContentResolver
. We are having problems instantiating ours.
Here's the stack trace we're getting:
java.lang.NullPointerException
at android.content.ContentProvider$Transport.enforceReadPermissionInner(ContentProvider.java:449)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:394)
at android.content.ContentProvider$Transport.query(ContentProvider.java:194)
at android.content.ContentResolver.query(ContentResolver.java:461)
at android.content.ContentResolver.query(ContentResolver.java:404)
at packagename.test.FooActivityTest.getNumCommentsForRecipient(FooActivityTest.java:84)
at packagename.test.FooActivityTest.testCommentEntryInternal(FooActivityTest.java:91)
at packagename.test.FooActivityTest.testCommentEntry1(FooActivityTest.java:108)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.access$000(InstrumentationTestCase.java:36)
at android.test.InstrumentationTestCase$2.run(InstrumentationTestCase.java:189)
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:1719)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
How can we resolve this problem?