microsoft-authentication-library-for-android: app crashes on getAccounts()

I am following the sample app to try and get silent login, but my app crashes on the getAccounts() function. if i clear the app storage no crash but if there is a previous login it crashes with the following stack trace

* Attempt to get a user and acquireTokenSilent
         * If this fails we do an interactive request
         */
        List<IAccount> accounts = null;
try {
            accounts = myApp.getAccounts(); //crashes here

            if (accounts != null && accounts.size() == 1) {
                /* We have 1 account */
               ...
}...
java.lang.RuntimeException: Unable to resume activity {jbloomfield.macroapp/jbloomfield.macroapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3822)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3854)
        at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:859)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at com.microsoft.identity.common.internal.cache.AccountCredentialCache.getAccountsWithKeys(AccountCredentialCache.java:168)
        at com.microsoft.identity.common.internal.cache.AccountCredentialCache.getAccounts(AccountCredentialCache.java:184)
        at com.microsoft.identity.common.internal.cache.AccountCredentialCache.getAccountsFilteredBy(AccountCredentialCache.java:205)
        at com.microsoft.identity.common.internal.cache.MsalOAuth2TokenCache.getAccounts(MsalOAuth2TokenCache.java:337)
        at com.microsoft.identity.client.PublicClientApplication.getAccounts(PublicClientApplication.java:330)
        at jbloomfield.macroapp.AuthManager.getAccs(AuthManager.java:124)
        at jbloomfield.macroapp.MainActivity.onResume(MainActivity.java:129)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1413)
        at android.app.Activity.performResume(Activity.java:7301)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 21 (8 by maintainers)

Most upvoted comments

Hey guys, I’ve implemented a workaround for this issue, I’m just catching the exception and when that happens I:

...
        } catch (e: Exception) {
            val msalPrefs = context.getSharedPreferences("com.microsoft.identity.client.account_credential_cache", Context.MODE_PRIVATE)
            msalPrefs.edit().clear().commit()
            Log.w("MSAL", e)
            callback.onError()
        }
...

I know that this is a super hacky solution, but It’s the only workaround that I’ve been able to find so far. If any of you can confirm that this fixes the issue for you as well, that would be super useful for me.

Thanks!

The issue is still present on msal 0.2.2

@4gus71n : ty for your workaround