Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails getSomething(int i) method doesn't compile

Can someone tell me why this Grails domain class will not compile (at runtime)?

class Person {
    String name

    String getSomething(int i) {
    }
}

I get this error when I run with grails run-app:

2008-12-27 15:26:33.955::WARN:  Failed startup of context org.mortbay.jetty.webapp.WebAppContext@187e184{/asrs2,C:\Steve\asrs2/web-app}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pluginManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
        at java.security.AccessController.doPrivileged(Native Method)
        at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy:67)
        at RunApp_groovy$_run_closure2_closure7.doCall(RunApp_groovy)
        at Init_groovy$_run_closure6.doCall(Init_groovy:131)
        at RunApp_groovy$_run_closure2.doCall(RunApp_groovy:66)
        at RunApp_groovy$_run_closure2.doCall(RunApp_groovy)
        at RunApp_groovy$_run_closure1.doCall(RunApp_groovy:57)
        at RunApp_groovy$_run_closure1.doCall(RunApp_groovy)
        at gant.Gant.dispatch(Gant.groovy:271)
        at gant.Gant.this$2$dispatch(Gant.groovy)
        at gant.Gant.invokeMethod(Gant.groovy)
        at gant.Gant.processTargets(Gant.groovy:436)
        at gant.Gant.processArgs(Gant.groovy:372)
Caused by: java.lang.NullPointerException
        at java.lang.Class.isAssignableFrom(Native Method)
        ... 13 more

If I change the method getSomething to doSomething then it works. Is getSomething(int i) somehow being treated as a bean method?

Follow up: This is a Grails bug which will be fixed in 1.2.

like image 716
Steve Kuo Avatar asked Dec 03 '22 08:12

Steve Kuo


1 Answers

Well, you've got two problems:

  1. Domain classes in Grails try to make sure that every property has a getter and setter during startup. It does this by looking for all the getters, and making sure an appropriate setter exists. So, if you have a getSomething(), you have to have a setSomething( def something ), even if there is no property "something" in the class. Really, by creating the getSomething() function, you've implied that there is such a property, and you must create a setSomething() as well.

  2. Getters do not take arguments. Yours does. Now I realize you weren't thinking this was a "getter" when you wrote it, but your naming of it makes it one.

Best bet? Don't use "get" or "set" or "is" prefixes unless you really are making a full property that can be gotten and set. I would also avoid "find" in Domain classes, as that has it's own set of generated methods.

like image 79
billjamesdev Avatar answered Dec 13 '22 05:12

billjamesdev