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

fix: null pointer on get app context #1878

Conversation

arthurgeron
Copy link

@arthurgeron arthurgeron commented Oct 23, 2023

Description

One Line Summary

Created ApplicationServiceUtil object, which checks if applicationService and appContext are valid references. Added Null checks to both ApplicationService and DeviceService, which were sources of null pointer Exceptions.
It also checks if the app service has initialized correctly right after the initialization.

Details

Motivation

OneSignal/react-native-onesignal#1587
Stack Trace:

Exception java.lang.RuntimeException: Unable to start receiver com.onesignal.notifications.receivers.FCMBroadcastReceiver: java.lang.reflect.InvocationTargetException
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4600)
  at android.app.ActivityThread.-$$Nest$mhandleReceiver
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2261)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loopOnce (Looper.java:211)
  at android.os.Looper.loop (Looper.java:300)
  at android.app.ActivityThread.main (ActivityThread.java:8410)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:559)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:954)
Caused by java.lang.reflect.InvocationTargetException:
  at java.lang.reflect.Constructor.newInstance0
  at java.lang.reflect.Constructor.newInstance (Constructor.java:343)
  at com.onesignal.common.services.ServiceRegistrationReflection.resolve (ServiceRegistration.kt:238)
  at com.onesignal.common.services.ServiceProvider.getServiceOrNull (ServiceProvider.kt:51)
  at com.onesignal.common.services.ServiceProvider.getService (ServiceProvider.kt:6)
  at com.onesignal.internal.OneSignalImp.initWithContext (OneSignalImp.kt:76)
  at com.onesignal.OneSignal.initWithContext (OneSignal.kt:2)
  at com.onesignal.notifications.receivers.FCMBroadcastReceiver.onReceive (FCMBroadcastReceiver.kt:32)
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4588)
Caused by java.lang.NullPointerException:
  at com.onesignal.core.internal.application.impl.ApplicationService.getAppContext (ApplicationService.kt:3)
  at com.onesignal.core.internal.preferences.impl.PreferencesService.getSharedPrefsByName (PreferencesService.kt:4)
  at com.onesignal.core.internal.preferences.impl.PreferencesService.get (PreferencesService.kt:38)
  at com.onesignal.core.internal.preferences.impl.PreferencesService.getString (PreferencesService.kt:13)
  at com.onesignal.common.modeling.ModelStore.load (ModelStore.kt:29)
  at com.onesignal.common.modeling.SimpleModelStore.<init> (SimpleModelStore.kt:4)
  at com.onesignal.core.internal.config.ConfigModelStore.<init> (ConfigModelStore.kt:12)

2nd StackTrace:

Exception java.lang.RuntimeException: Unable to start receiver com.onesignal.notifications.receivers.NotificationDismissReceiver: java.lang.NullPointerException
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4895)
  at android.app.ActivityThread.-$$Nest$mhandleReceiver
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2422)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loopOnce (Looper.java:226)
  at android.os.Looper.loop (Looper.java:313)
  at android.app.ActivityThread.main (ActivityThread.java:8775)
  at java.lang.reflect.Method.invoke
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:571)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1067)
Caused by java.lang.NullPointerException:
  at com.onesignal.core.internal.application.impl.ApplicationService.getAppContext (ApplicationService.kt:3)
  at com.onesignal.core.internal.device.impl.DeviceService.packageInstalledAndEnabled (DeviceService.kt:3)
  at com.onesignal.core.internal.device.impl.DeviceService.isGMSInstalledAndEnabled (DeviceService.kt:3)
  at com.onesignal.core.internal.device.impl.DeviceService.supportsGooglePush (DeviceService.kt:9)
  at com.onesignal.core.internal.device.impl.DeviceService.getDeviceType (DeviceService.kt:10)
  at com.onesignal.core.internal.device.impl.DeviceService.isAndroidDeviceType (DeviceService.kt:1)
  at com.onesignal.location.LocationModule$register$1.invoke (LocationModule.kt:3)
  at com.onesignal.location.LocationModule$register$1.invoke (LocationModule.kt:1)
  at com.onesignal.common.services.ServiceRegistrationLambda.resolve (ServiceRegistration.kt:13)
  at com.onesignal.common.services.ServiceProvider.getServiceOrNull (ServiceProvider.kt:51)
  at com.onesignal.common.services.ServiceProvider.getService (ServiceProvider.kt:6)
  at com.onesignal.common.services.ServiceRegistrationReflection.resolve (ServiceRegistration.kt:208)
  at com.onesignal.common.services.ServiceProvider.getServiceOrNull (ServiceProvider.kt:51)
  at com.onesignal.common.services.ServiceProvider.getService (ServiceProvider.kt:6)
  at com.onesignal.common.services.ServiceRegistrationReflection.resolve (ServiceRegistration.kt:208)
  at com.onesignal.common.services.ServiceProvider.getServiceOrNull (ServiceProvider.kt:51)
  at com.onesignal.common.services.ServiceProvider.getService (ServiceProvider.kt:6)
  at com.onesignal.internal.OneSignalImp.initWithContext (OneSignalImp.kt:233)
  at com.onesignal.OneSignal.initWithContext (OneSignal.kt:2)
  at com.onesignal.notifications.receivers.NotificationDismissReceiver.onReceive (NotificationDismissReceiver.kt:20)
  at android.app.ActivityThread.handleReceiver (ActivityThread.java:4886)

Scope

OPTIONAL - Other

Testing

Unit testing

Manual testing

Affected code checklist

  • Notifications
    • Display
    • Open
    • Push Processing
    • Confirm Deliveries
  • Outcomes
  • Sessions
  • In-App Messaging
  • REST API requests
  • Core
  • Public API changes

Checklist

Overview

  • I have filled out all REQUIRED sections above
  • PR does one thing
    • Fix NullPointer exception in DeviceService.kt and PreferencesService.kt
  • Any Public API changes are explained in the PR details and conform to existing APIs

Testing

  • I have included test coverage for these changes, or explained why they are not needed
  • All automated tests pass, or I explained why that is not possible
  • I have personally tested this on my device, or explained why that is not possible

Final pass

  • Code is as readable as possible.
    • Simplify with less code, followed by splitting up code into well named functions and variables, followed by adding comments to the code.
  • I have reviewed this PR myself, ensuring it meets each checklist item
    • WIP (Work In Progress) is ok, but explain what is still in progress and what you would like feedback on. Start the PR title with "WIP" to indicate this.

This change is Reviewable

@jennantilla jennantilla deleted the branch OneSignal:fix/concurrent_modification_exception November 3, 2023 21:49
@jennantilla jennantilla closed this Nov 3, 2023
@passerby-yyds
Copy link

What was the reason for closing this PR?

@arthurgeron
Copy link
Author

What was the reason for closing this PR?

Because according to them, contrary to their documentation, you shouldn't call initWithContext both on native and on JS, which would be, again according to them, the source of this error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants