Alex
Alex

Reputation: 8303

Saving a Grails GORM/Domain class results in a `NullPointerException`

I have a service that's responsible for creating the relevant account hierarchy when a registration page is successfully filled out :

    def userRole = Role.findByAuthority("ROLE_USER")
    final account = new Account(email: command.email, password: command.password)
    account.save(flush: true)

    final publisher = new Publisher(name: command.name) //, account: account)
    publisher.save(flush: true)

    final accountRole = AccountRole.create account, userRole

Regardless of what I do, the AccountRole.create account, userRole fails at this line:

static AccountRole create(Account account, Role role) {
    def instance = new AccountRole(account: account, role: role)
    instance.save()  // throws NullPointerException
    instance
}

I have tried new AccountRole(account: account, role: userRole), but this too fails when I manually call save. The Account and AccountRole are classes generated (and tweaked slightly) by the Grails 3 Spring Security plugin.

Here's the stacktrace for the AccountRole.create(...) call that fails:

java.lang.reflect.InvocationTargetException: null
    at grails.plugin.springsecurity.web.UpdateRequestContextHolderExceptionTranslationFilter.doFilter(UpdateRequestContextHolderExceptionTranslationFilter.groovy:64)
    at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53)
    at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62)
    at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58)
    at grails.plugin.springsecurity.web.filter.DebugFilter.invokeWithWrappedRequest(DebugFilter.groovy:102)
    at grails.plugin.springsecurity.web.filter.DebugFilter.doFilter(DebugFilter.groovy:69)
    at grails.plugin.springsecurity.web.UpdateRequestContextHolderExceptionTranslationFilter.doFilter(UpdateRequestContextHolderExceptionTranslationFilter.groovy:64)
    at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53)
    at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62)
    at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: null
    at package.AccountRole.create(AccountRole.groovy:48)
    at package.RegistrationService.$tt__handleNewRegistration(RegistrationService.groovy:25)

Version information:

| Grails Version: 3.1.5
| Groovy Version: 2.4.6
| JVM Version: 1.8.0_73

Please save me from my misery! I've spent hours on this now :(

Upvotes: 2

Views: 1211

Answers (1)

Burt Beckwith
Burt Beckwith

Reputation: 75671

There's not really enough info to be sure, but it's unlikely that instance is null since you just created it with a constructor call, but you should check that to be sure, e.g.

static AccountRole create(Account account, Role role) {
   def instance = new AccountRole(account: account, role: role)
   println "instance null? ${instance == null}"
   instance.save()  // throws NullPointerException
   instance
}

It's more likely that a problem during validation is causing the exception, so my guess is that there's something wrong with either userRole or account. Check that userRole was correctly retrieved and that there aren't validation errors from the Account save() call:

def account = new Account(email: command.email, password: command.password)
account.save(flush: true)
if (account.hasErrors()) {
   log.warn "Failed to save Account $account: $account.errors"
}

Upvotes: 2

Related Questions