Following the GORM docs I tried to use the following domain class with Grails 2.2.1:
package grailscompositiontest
class ScafPerson {
String name
ScafAddress homeAddress
ScafAddress workAddress
static constraints = {
name(nullable: false, blank: false)
}
static embedded = ['homeAddress', 'workAddress']
}
class ScafAddress {
String number
String code
}
The controller just uses scaffolding:
package grailscompositiontest
class ScafPersonController {
static scaffold = true
}
Unfortunately this does not work, it triggers a server error as soon as I browse to the "create" view:
URI: /GrailsCompositionTest/scafPerson/create
Class: java.lang.NullPointerException
Message: Cannot get property 'id' on null object
Any idea what I am doing wrong?
I ran into this same problem the other day. I believe there may be a bug in the templates used by the scaffolding functionality. You can either update the template or if you don't want to muck with the templates, run generate-all as Benoit mentioned then fix the generated view.
To update template:
sb << ' value="${' << domainInstance << '.' << property.name << '}"'
and change to
sb << ' value="${' << domainInstance << '?.' << property.name << '}"'
To fix the generated view:
<g:field name="id" type="number" value="${scafAddressInstance.id}" required=""/> <g:field name="version" type="number" value="${scafAddressInstance.id}" required=""/>
and change to
<g:field name="id" type="number" value="${scafAddressInstance?.id}" required=""/> <g:field name="version" type="number" value="${scafAddressInstance?.id}" required=""/>
Note you'll do it twice since you have homeAddress/workAddress
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With