2

I'm experiencing a weird bug in my application. I have a MyApplication class extending Application. In its onCreate() method I initialize some 3rd party libs which require API keys stored in a string resource file. Basically, it works fine on devices that I can test with: Nexus S and Galaxy SIII. But I got a couple entries from crash reporting system and if I run Robolectric test for MyApplication.onCreate() I'm getting NullPointerException from getString() method.

I wonder if somebody had same issue and what might be the cause?

Log output for test (crash report also refers to the same line in setupAnalyticsCrashReportingAndThirdPartySdks):

java.lang.NullPointerException
at org.robolectric.shadows.ShadowResources.getResName(ShadowResources.java:100)
at org.robolectric.shadows.ShadowResources.getString(ShadowResources.java:130)
at android.content.res.Resources.getString(Resources.java)
at org.robolectric.shadows.ShadowContext.getString(ShadowContext.java:54)
at android.content.Context.getString(Context.java)
at com.myapp.android.MyApplication.setupAnalyticsCrashReportingAndThirdPartySdks(MyApplication.java:113)
at com.myapp.android.MyApplication.onCreate(MyApplication.java:42)
at com.myapp.android.util.UtilitiesTest.setUp(UtilitiesTest.java:44)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:106)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)

Update: A little piece of the code:

public class MyApplication extends Application

{

private static MyApplication _instance;

@Override
public void onCreate()
{
    super.onCreate();

    setupAnalyticsCrashReportingAndThirdPartySdks();
}

public MyApplication()
{
    _instance = this;
}

public static MyApplication getInstance()
{
    return _instance;
}

private void setupAnalyticsCrashReportingAndThirdPartySdks()
{
    String env = getString(R.string.environment); // Crash here
    if (env != null && env.equalsIgnoreCase("dev"))
    {
        FlurryAnalytics.setDebugMode(true);
    }
}

}

slavkoder
  • 156
  • 6
  • I'd recommend adding a bit of your code to the question so we can get a bit more context. – DigCamara May 08 '13 at 23:17
  • Does reading the source for that library help? Not sure this is the right code: http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/robolectric/2.0-alpha-2/org/robolectric/shadows/ShadowResources.java#ShadowResources.getResName%28int%29 – jarmod May 08 '13 at 23:29
  • @DigCamara see update for code. In my opinion it should work properly, but something strange is happening there. – slavkoder May 10 '13 at 19:24
  • Can you post your strings.xml as well? (it should be in src\res\values). If you haven't defined environment in there, it should (I'm guessing here) be part of the library's resources. – DigCamara May 10 '13 at 19:29
  • @DigCamara the file is called res/values/settings.xml, but it shouldn't be a problem, since it's basically just another string resource file. And this value is being read properly from Activity context in another place of the app. I'll try to create a separate project with Robolectric and test different options for string resources, may be I can find out something that way. – slavkoder May 10 '13 at 23:25

0 Answers0