2

When updating my app widget I need to check the provider class name. appWidgetManager.getAppWidgetInfo(widgetId).provider.className

But sometimes I face the exception Fatal Exception: java.lang.NullPointerException: Attempt to read from field 'android.content.ComponentName android.appwidget.AppWidgetProviderInfo.provider' on a null object reference

And I don't know why. Documentation of getAppWidgetInfo says:

If the appWidgetId has not been bound to a provider yet, or you don't have access to that appWidgetId, null is returned.

I guess my case is "the appWidgetId has not been bound to a provider", but how to catch the moment when a widget is bound already? Or maybe the problem is something else?

IL_Agent
  • 707
  • 3
  • 16
  • Can you provide more of a [mcve], showing where `appWidgetManager.getAppWidgetInfo(widgetId).provider.className` appears, along with the full stack trace? – CommonsWare May 14 '21 at 12:39
  • @CommonsWare that's not easy because the issue is floating and nobody guarantees reproducing. Maybe has sense the fact that I call `getAppWidgetInfo` in a WorkManager task which starts immediately in `onUpdate`. – IL_Agent May 14 '21 at 15:12
  • Finally I rewrote my code and got rid of `getAppWidgetInfo` call but the question is still open. – IL_Agent May 14 '21 at 15:14
  • "Maybe has sense the fact that I call getAppWidgetInfo in a WorkManager task which starts immediately in onUpdate" -- that may be [a bigger problem](https://issuetracker.google.com/issues/115575872#comment4). "that's not easy because the issue is floating and nobody guarantees reproducing" -- that does not prevent you from showing where `appWidgetManager.getAppWidgetInfo(widgetId).provider.className` appears, along with the full stack trace. – CommonsWare May 14 '21 at 15:23

0 Answers0