chrisburke.io
chrisburke.io

Reputation: 1507

Grails save() failure; can't figure out why

I am having a problem saving a Domain object. Below is my Controller code:

def onContactRequest = {

    if(request.method == 'POST') {
        if(User.findByUserTelephone(params.userTelephone)) {
            User thisUser = User.findByUserTelephone(params.userTelephone)
            Contact thisContact = new Contact()

            thisContact.setContact(thisUser)

            println("This Contact: " + thisContact.getContact());

            thisContact.setBelongsTo(request.user)

            println("This User: " + request.user)

            if(thisContact.save(flush: true)) {
                render(thisContact.belongsTo.userName + " just requested " + thisContact.getContact().userName )
            } else {
                render("There was a problem saving the Contact.")
                if( !thisContact.save() ) {
                   thisContact.errors.each {
                        println it
                   }
                }
            }
        } else {
            User thisUser = new User()
            thisUser.setUserName("Not Set")
            thisUser.setUserTelephone(params.userTelephone)
            thisUser.save()
            Contact thisContact = new Contact()
            thisContact.setContact(thisUser)
            thisContact.setBelongsTo(request.user)
            if(thisContact.save(flush: true)) {
                render(thisContact.belongsTo.userName + " just requested " + thisContact.getContact().userName )
            } else {
                render("There was a problem saving the Contact.")
                if( !thisContact.save() ) {
                   thisContact.errors.each {
                        println it + "\n"
                   }
                }
            }

        }
    } else {

    }

The error message is printed with the following code; hence it's very ugly:

if( !thisContact.save() ) {
    thisContact.errors.each {
        println it + "\n"
    }
}

From what I can tell, it's complaining that either the Contact or User instance is null; however that can't be true (look below)

This Contact: org.icc.callrz.User.User : 2
This User: org.icc.callrz.User.User : 1

Field 'user' in org.icc.callrz.Contact.Contact is:

static belongsTo = [
    user:                                   User
]

Error detail below:

org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'org.icc.callrz.Contact.Contact' on field 'user': rejected value [null]; codes [org.icc.callrz.Contact.Contact.user.nullable.error.org.icc.callrz.Contact.Contact.user,org.icc.callrz.Contact.Contact.user.nullable.error.user,org.icc.callrz.Contact.Contact.user.nullable.error.org.icc.callrz.User.User,org.icc.callrz.Contact.Contact.user.nullable.error,contact.user.nullable.error.org.icc.callrz.Contact.Contact.user,contact.user.nullable.error.user,contact.user.nullable.error.org.icc.callrz.User.User,contact.user.nullable.error,org.icc.callrz.Contact.Contact.user.nullable.org.icc.callrz.Contact.Contact.user,org.icc.callrz.Contact.Contact.user.nullable.user,org.icc.callrz.Contact.Contact.user.nullable.org.icc.callrz.User.User,org.icc.callrz.Contact.Contact.user.nullable,contact.user.nullable.org.icc.callrz.Contact.Contact.user,contact.user.nullable.user,contact.user.nullable.org.icc.callrz.User.User,contact.user.nullable,nullable.org.icc.callrz.Contact.Contact.user,nullable.user,nullable.org.icc.callrz.User.User,nullable]; arguments [user,class org.icc.callrz.Contact.Contact]; default message [Property [{0}] of class [{1}] cannot be null]

Edit: I have no problem creating Contact domain objects using the 'generate-all' code.

SOLUTION: I had a look at the code in the view, and it looked like to create the ID was used, so I changed the code to:

thisContact.user.id = request.user.id

However, then I got an error: java.lang.NullPointerException: Cannot set property 'id' on null object but the output of println request.user was not blank so I wasn't sure why that was appearing.

I then changed the offending line to:

thisContact.user = request.user

Now everything is working. :)

Upvotes: 2

Views: 1354

Answers (1)

Oliver Tynes
Oliver Tynes

Reputation: 964

Try replacing:

thisContact.setBelongsTo(request.user)

With:

thisContact.user = thisUser

The syntax you are using is wrong as far as I know, not to mention that you construct thisUser and then go on to use request.user instead.

Upvotes: 1

Related Questions