CodexBloom - Programming Q&A Platform

how to to Pass Parcelable Object between Fragments using Safe Args in Android 14 - Null Object Reference

πŸ‘€ Views: 479 πŸ’¬ Answers: 1 πŸ“… Created: 2025-08-26
android kotlin navigation Kotlin

I've looked through the documentation and I'm still confused about Hey everyone, I'm running into an issue that's driving me crazy. I'm updating my dependencies and Hey everyone, I'm running into an issue that's driving me crazy. I'm working with an scenario when trying to pass a Parcelable object between two fragments using Safe Args in my Android application targeting API level 34. I have a `User` data class that implements `Parcelable`, which I am trying to pass from `UserFragment` to `UserDetailFragment`. Here’s how I set up the navigation action: In `nav_graph.xml`, I defined the action like this: ```xml <action android:id="@+id/action_userFragment_to_userDetailFragment" app:destination="@id/userDetailFragment" app:argType="com.example.app.User" /> ``` In my `UserFragment`, I'm attempting to navigate like this: ```kotlin val user = User(name = "John Doe", age = 28) val action = UserFragmentDirections.actionUserFragmentToUserDetailFragment(user) navigate(action) ``` However, when I arrive at `UserDetailFragment`, I attempt to retrieve the user object as follows: ```kotlin val args: UserDetailFragmentArgs by navArgs() val user = args.user ``` At this point, I'm getting a `NullPointerException`, which indicates that `user` is null. I've verified that the `User` class correctly implements the `Parcelable` interface with all necessary methods (`writeToParcel`, `describeContents`, and the `CREATOR`). I've also checked for any potential issues with the NavGraph setup. I suspect that the scenario might be related to how I am declaring the argument type in the NavGraph, but I'm not sure how to correct it. I've tried cleaning the project and rebuilding, but that hasn't helped. Any insights on what could be going wrong here would be greatly appreciated! My development environment is Ubuntu. Am I missing something obvious? How would you solve this? This is my first time working with Kotlin 3.10. Is there a simpler solution I'm overlooking?