How do I handle an empty List in Grails/GORM?
grails, grails-orm
Solution
In your test parent.children will always be null (children wont be initialized until you add the first one). So you could change your test to:
assertNull parent.children
The children will be initialized when you save the Parent (regardless if children are added) or when children are added. If you want it to always be initialized you can do it manually in the Parent when you define the children:
List<Children> children = new ArrayList<Children>()
Problem
I'm trying to get a one-to-many relationship working with grails/gorm. I don't understand how to handle an empty list. Here is my domain class: ``` class Parent { List children static hasMany = [children: Children] } ``` ` Here is my test: ``` void testEmptyChildren() { def parent = new Parent() assert 0, parent.children.size() } ``` ` This fails with "java.lang.NullPointerException: Cannot invoke method size() on null object" What am I supposed to do to handle an empty List?