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

NullPointerException in PortalFragment; potential problems with Fragment-recreations #112

Closed
fkirc opened this issue Jan 21, 2022 · 10 comments · Fixed by #114
Closed

NullPointerException in PortalFragment; potential problems with Fragment-recreations #112

fkirc opened this issue Jan 21, 2022 · 10 comments · Fixed by #114

Comments

@fkirc
Copy link

fkirc commented Jan 21, 2022

Dear ionic-team,

After using Portals successfully in production for multiple weeks, I noticed hundreds of NullPointerExceptions in my sentry.io crash-reports. Specifically, a NullPointerException happened in the following line:
https://github.com/ionic-team/ionic-portals/blob/main/android/IonicPortals/src/main/kotlin/io/ionic/portals/PortalFragment.kt#L99

Here is the stacktrace that was reported by sentry.io:

java.lang.NullPointerException: null
at io.ionic.portals.PortalFragment.load(PortalFragment.kt:99)
at io.ionic.portals.PortalFragment.onViewCreated(PortalFragment.kt:44)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2987)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:546)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:502)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1435)
at android.app.Activity.performStart(Activity.java:8231)
at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3853)
at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8625)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

I suspect that this happens when a PortalFragment gets instantiated without an instance of the Portal-class.
For example, such erroneous instantiations might happen when the following empty default-constructor of a PortalFragment gets invoked:
https://github.com/ionic-team/ionic-portals/blob/main/android/IonicPortals/src/main/kotlin/io/ionic/portals/PortalFragment.kt#L27

This makes me question whether there is a potential problem with Fragment-recreations.
The Android-OS might destroy and recreate Fragments at any time, and this might trigger the NullPointerException.

Perhaps I could "fix" the bug by adding more null-checks to the problematic code, but my goal would be to find a root-cause instead of only adding null-checks.

@carlpoole
Copy link
Member

Thanks for reporting, what version of Portals are you seeing the errors from?

@fkirc
Copy link
Author

fkirc commented Jan 21, 2022

Previously, I was seeing those errors with 0.2.1.
However, today I deployed a production-update to 0.3.1, so I will monitor during the upcoming days whether the error still persists with 0.3.1.
From what I have seen, the relevant code-snippets in PortalFragment seem to be unchanged since 0.2.1.

@carlpoole
Copy link
Member

carlpoole commented Jan 21, 2022

Someone else reported NPE crashes that were similar that we made a fix for in #95 so I was curious if that might resolve it for you. That fix is in 0.3.1.

@fkirc
Copy link
Author

fkirc commented Jan 21, 2022

Someone else reported NPE crashes that were similar that we made a fix for in #95 so I was curious if that might resolve it for you. That fix is in 0.3.1.

Yeah this seems like a related fix. I will let you know if something happens with 0.3.1, if nothing happens then I will close this issue within a week.

@fkirc
Copy link
Author

fkirc commented Jan 26, 2022

Unfortunately I received the same stacktrace again with my new app-version, which means that the error is not fixed with 0.3.1.
I am thinking about forking to add some more null-checks, but perhaps it is necessary to rethink the PortalFragment-instantiation.

@fkirc
Copy link
Author

fkirc commented Jan 26, 2022

Small sidenote: I embedded a PortalView into a larger RelativeLayout like this:

    <io.ionic.portals.PortalView
        app:portalId="MyPortalID"
        android:layout_below="@id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

So I instantiate Portals via XML-layouts, but not directly via Kotlin-code.
I still guess that the following empty-constructor leads to the crash:
https://github.com/ionic-team/ionic-portals/blob/main/android/IonicPortals/src/main/kotlin/io/ionic/portals/PortalFragment.kt#L27

@carlpoole
Copy link
Member

Thanks for the feedback, I will take a look and see if I can pinpoint the cause.

@carlpoole
Copy link
Member

I think I found the cause and pushed a fix. We will release this asap

@carlpoole
Copy link
Member

Look out for Portals 0.4.1 - the fix is published! Thanks for reporting. Please let us know if you still encounter an issue

@fkirc
Copy link
Author

fkirc commented Jan 28, 2022

Thank you for publishing a fix!
I will release an update of my app in the upcoming days, then I would notify you if there are still any issues.

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 a pull request may close this issue.

2 participants