I've defined a model that represents a meeting, with a menu and a workout plan. The menu has a list of courses, each of which has a list of meals, and the workout plan has a list of exercises.
[
{
"menu": {
"courses": [
{
"meals": [
{
...
}
],
}
],
},
"workoutPlan": {
"exercises": [
{
...
},
]
},
}
]
in that way:
PopulatedMeeting.kt
data class PopulatedMeeting(
@Embedded val meeting: MeetingEntity,
@Relation(
parentColumn = "menuId",
entityColumn = "id",
entity = MenuEntity::class
)
val menu: PopulatedMenu,
@Relation(
parentColumn = "workoutPlanId",
entityColumn = "id",
entity = WorkoutPlanEntity::class
)
val workoutPlan: PopulatedWorkoutPlan
)
PopulatedMenu.kt
data class PopulatedMenu(
@Embedded
val menu: MenuEntity,
@Relation(
parentColumn = "id",
entityColumn = "id",
associateBy = Junction(
value = MenuCourseCrossRef::class,
parentColumn = "menu_id",
entityColumn = "course_id"
),
entity = CourseEntity::class
)
val courses: List<PopulatedCourse>
)
When I run the app, I'm getting this execption:
java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter menu
The reason is most likely that you have a Meeting that does not reference a Menu.
Consider the following data which results in:-
The database, via App inspection has:-
The MeetingEntity table populated with:-
The MenuEntity table populated with:-
Hence the menu will be null when retrieving a PopulatedMeeting.
The following activity code was used to create the above:-
The log when running the above includes:-
i.e. the PopulatedMeetings with a valid reference to a Menu are fine and utilise your PopulatedMeeting and PopulatedMenu (albeit it that the related Workoutplan was excluded for convenience/brevity).
You may wish to consider enforcing Referential Integrity (e.g. so that the menu_id cannot be a value that does not reference an actual menu).
To enforce referential integrity you can setup Foreign Keys e.g. if the following were coded:-
Then the code above would instead fail with the following in the log (and more importantly when trying to insert the errant reference):-
:-
In which case replacing the insert with insertIgnoringFKConflict for the 3 Meetings results in no failure and the log including:-