Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullpointerException when registering a SyncStatusObserver in Application.onCreate() #1044

Closed
ndefeijter opened this issue Apr 23, 2014 · 3 comments

Comments

@ndefeijter
Copy link

java.lang.RuntimeException: java.lang.NullPointerException
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NullPointerException
    at android.content.ContentResolver.addStatusChangeListener(ContentResolver.java:1797)
    at a.b.c.XyzApplication.onCreate(XyzApplication.java:45)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:164)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:430)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236)
    ... 16 more

The MyApplication.onCreate() method is very simple and looks like this:

    @Override
    public void onCreate() {
        super.onCreate();
        // Watch for sync state changes
        final int mask = ContentResolver.SYNC_OBSERVER_TYPE_PENDING | ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE;
        this.syncObserverHandle = ContentResolver.addStatusChangeListener(mask, this.syncStatusObserver);
    }

I guess it has something to do with the getContentService() method returning null:

    public static Object addStatusChangeListener(int mask, final SyncStatusObserver callback) {
        if (callback == null) {
            throw new IllegalArgumentException("you passed in a null callback");
        }
        try {
            ISyncStatusObserver.Stub observer = new ISyncStatusObserver.Stub() {
                public void onStatusChanged(int which) throws RemoteException {
                    callback.onStatusChanged(which);
                }
            };
            getContentService().addStatusChangeListener(mask, observer);
            return observer;
        } catch (RemoteException e) {
            throw new RuntimeException("the ContentService should always be reachable", e);
        }
    }

Running this code on a device or emulator is fine but running in Robolectric results in a NullPointerException.

Is the ContentService not yet available in Application.onCreate()?

@erd
Copy link
Member

erd commented Oct 14, 2015

I suspect that we need to shadow addStatusChangeListener.

@Jeff-Stapleton
Copy link

Any progress on this?

@xian xian removed the 2.4 label Jan 13, 2017
@utzcoz
Copy link
Member

utzcoz commented Dec 12, 2022

Please test it with latest Robolectric again to verify whether it fixes this issue. The Robolectric version related to this issue is too old. If this issue still exits with latest Robolectric 4.9, feel free to reopen this issue and attach a minimal reproducable app that we can use to reproduce this issue and for later investigation. Thanks.

@utzcoz utzcoz closed this as completed Dec 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants