Quick Fix: NullPointerException with StyleResolver, Robolectric

Brian Terczynski
1 min readOct 10, 2022

This is just a quick little note about getting Robolectric set up in a new project. This gets me every time, even though the Robolectric documentation clearly states to do this! I just keep forgetting.

When I want to add Robolectric to a new Android project, I do the usual and add Robolectric and Espresso as testImplementation clauses:

testImplementation 'org.robolectric:robolectric:<version>'
testImplementation 'androidx.test.espresso:espresso-core:<version>'

I then write my test class and try to run it. When I do, I get an exception related to theming/styling:

java.lang.NullPointerException
at org.robolectric.res.StyleResolver.getParent(StyleResolver.java:92)
at org.robolectric.res.StyleResolver.getAttrValue(StyleResolver.java:33)
at org.robolectric.res.ThemeStyleSet.getAttrValue(ThemeStyleSet.java:17)
at org.robolectric.shadows.ShadowLegacyAssetManager.findAttributeValue(ShadowLegacyAssetManager.java:1280)
at org.robolectric.shadows.ShadowLegacyAssetManager.buildTypedValue(ShadowLegacyAssetManager.java:1142)
at org.robolectric.shadows.ShadowLegacyAssetManager.attrsToTypedArray(ShadowLegacyAssetManager.java:1192)
at org.robolectric.shadows.ShadowResources$ShadowLegacyTheme.obtainStyledAttributes(ShadowResources.java:305)
at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java)
at android.content.Context.obtainStyledAttributes(Context.java:362)
at androidx.appcompat.widget.TintTypedArray.obtainStyledAttributes(TintTypedArray.java:54)
at androidx.appcompat.app.AppCompatDelegateImpl.attachToWindow(AppCompatDelegateImpl.java:801)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureWindow(AppCompatDelegateImpl.java:779)
at androidx.appcompat.app.AppCompatDelegateImpl.onCreate(AppCompatDelegateImpl.java:506)
at androidx.appcompat.app.AppCompatActivity$2.onContextAvailable(AppCompatActivity.java:131)
at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable(ContextAwareHelper.java:99)
at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:322)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:273)
at com.brianterczynski.testrobolectricandgradle.MainActivity.onCreate(MainActivity.kt:20)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at org.robolectric.android.internal.RoboMonitoringInstrumentation.callActivityOnCreate(RoboMonitoringInstrumentation.java:279)
at org.robolectric.android.controller.ActivityController.lambda$create$0(ActivityController.java:105)
at org.robolectric.shadows.ShadowPausedLooper.runPaused(ShadowPausedLooper.java:200)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:105)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:110)
at org.robolectric.android.controller.ActivityController.setup(ActivityController.java:267)
at
...

It turns out the reason is because resources like themes, styles, etc. are not being found while running the unit test. To fix that, I need set includeAndroidResources as a test option for unit tests.

testOptions {
unitTests {
includeAndroidResources = true
}
}

And that fixes the issue.

So I’m mostly writing this post to remind myself (maybe others) not to forget to add includeAndroidResources (again, it’s in the Robolectric documentation, I just keep forgetting). Or in other words, if you get a NullPointerException with resources while running Robolectric, then includeAndroidResources is a likely fix.

--

--

Brian Terczynski

Documenting my learnings on my journey as a software engineer.