Search code examples
grailsgrails-plugin

grails new Security Plugin giving null pointer exception


I added the following code in Bootstrap.groovy directly following the tutorial on the Security Core Plugin, and I'm getting a null pointer exception from Bootstrap.groovy. Any idea what is wrong?

Thanks, Ray

P.S. I added the User and Role classes using the recommended plugin's recommended s2-quickstart command, which generates the User, Role, and UserRole domain classes. The next step is to set up some example users in Bootstrap, which is failing.


Bootstrap.groovy:

       println "Creating roles user, leader, and admin"
       def userRole = Role.findByAuthority("ROLE_USER") ?: new Role(authority: "ROLE_USER").save()
       def leaderRole = Role.findByAuthority("ROLE_LEADER") ?: new Role(authority: "ROLE_LEADER").save()
       def adminRole = Role.findByAuthority("ROLE_ADMIN") ?: new Role(authority: "ROLE_ADMIN").save()

       println "Creating users called user, leader, and admin"
       def user = new User(username: "user", password: springSecurityService.encodePassword("abc"), enabled: true)
       def leader = new User(username: "leader", password: springSecurityService.encodePassword("abc"), enabled: true)
       def admin = new User(username: "admin", password: springSecurityService.encodePassword("abc"), enabled: true)

       println "Now joining users and their roles"
       UserRole.create(user, userRole)  <------------- FAILING HERE
       UserRole.create(leader, leaderRole)
       UserRole.create(admin, adminRole)
       println "All Done Creating users & roles"

Which is producing the following NullPointerException:

Creating roles user, leader, and admin

Creating users called user, leader, and admin

Now joining users and their roles

2011-08-23 14:34:10,762 [main] ERROR context.GrailsContextLoader  - Error executing bootstraps: null

java.lang.NullPointerException
    at $Proxy18.save(Unknown Source)
      **at momentum.UserRole.create(UserRole.groovy:32)**
    at momentum.UserRole.create(UserRole.groovy)
    at momentum.UserRole$create.call(Unknown Source)
    **at BootStrap$_closure1.doCall(BootStrap.groovy:168)**

The lines are as follows:

BootStrap.groovy line 168: UserRole.create(user, userRole)

UserRole.groovy line 32: new UserRole(user: user, role: role).save(flush: flush, insert: true)


Solution

  • You're never saving the new users.

    user.save()
    leader.save()
    admin.save()