spock99
spock99

Reputation: 1184

Error in grails command object

The strange part about this error is sometimes the page generates the error, and sometimes it doesn't. I can't figure out what causes it or why:

errors.GrailsExceptionResolver ClassCastException occurred when processing request: [GET] /birthFamily/aboutYouLifestyle
java.util.LinkedHashMap cannot be cast to groovy.lang.Closure. Stacktrace follows:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to groovy.lang.Closure
    at com.nrfa.LifestyleCommand$__clinit__closure1.doCall(Wizard.groovy:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:724)

The offending command object:

@Validateable
public  class LifestyleCommand {
    Integer applicantNumber;
    Smoke smoke
    Drink drink
    Occupation occupation
    Set <String> hobby
    String occupationDetails

    static constraints = {
        importFrom Applicant, include:["smoke", "drink", "occupation", "occupationDetails", "applicantNumber"]
    }
}

Line 302 in the stack trace is the 'importFrom Applicant' line in the command object constraints. What does 'java.util.LinkedHashMap cannot be cast to groovy.lang.Closure' indicate?

When I replace the constraints as a copy of those found in the Applicant domain, it also works fine:

static constraints = {

    applicantNumber (blank:false, nullable:false, range:1..2)
    smoke (blank:true, nullable:true)
    drink (blank:true, nullable:true)
    occupation (blank:true, nullable:true)
    occupationDetails (blank:true, nullable:true, maxSize:150)
}

EDIT:

Here is the relevant portion of the Applicant, which is a domain object:

class Applicant {

    static hasMany = [hobby:Hobby, ethnicity:Ethnicity]

    Integer applicantNumber
    String gender
    Integer yearBorn
    EyeColor eyeColor
    HairColor hairColor
    Integer heightFeet
    Integer heightInches

    static constraints = {
        applicantNumber (blank:false, nullable:false, range:1..2)
        gender (blank:true, nullable:true, validator: { value ->
            if (value != null && value != '' && (value != "M" && value != "F")) {
                return 'applicant.invalid.gender'
            }
        })
        yearBorn (blank:true, nullable:true, validator: { value ->
            if (value != null && (value < 1920 || value > 2014)) {
                return 'applicant.invalid.yearBorn'
            }
        })
        eyeColor (blank:true, nullable:true)
        hairColor (blank:true, nullable:true)
        smoke (blank:true, nullable:true)
        drink (blank:true, nullable:true)
        heightFeet (blank:true, nullable:true, validator: { value ->
            if (value != null && (value < 3 || value > 7)) {
                return 'applicant.invalid.heightFeet'
            }
        })
        heightInches (blank:true, nullable:true, validator: { value ->
            if (value != null && (value < 1 || value > 12)) {
                return 'applicant.invalid.heightInches'
            }
        })
    }
}

Wizard is my controller. There is an action to process a form request which submits the data to be populated into the LifestyleCommand object. I'm just submitting the page back with none of the fields filled out, so everything is null/empty which is valid.

I'm running grails 2.2.4

Upvotes: 2

Views: 1733

Answers (1)

rxn1d
rxn1d

Reputation: 1266

As was pointed out in the comments you probably should remove occupation and occupationDetails from include block of LifestyleCommand constraints (there are no constaints of that fields in Applicant).

Upvotes: 2

Related Questions