Sunday, June 14, 2009

grails Day 1 part 2

... then I hit a brick wall.

following the second Mastering Grails tutorial, I create an class Trip which has a object Airline, and an Airline class:

Class Trip {
String destination
...
Airline airline
}
class Airline{
static constraints = {
}

String name
String url
String frequentFlyer
String notes

static hasMany = [trip:Trip]
}
and all the other stuff just as the tutorial requires...
I start up grails and browse to localhost:8080/trip-planner/
I see an empty table of trips, including the new Airline field. so I click on 'create new' and get:
Error 500: Error processing GroovyPageView: Error executing tag : org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error evaluating expression [tripInstance?.airline?.id] on line [34]: java.lang.NoSuchMethodException: Unknown property 'airline'
Servlet: grails
URI: /trip-planner2/grails/trip/create.dispatch
Exception Message: Unknown property 'airline'
Caused by: Error processing GroovyPageView: Error executing tag : org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error evaluating expression [tripInstance?.airline?.id] on line [34]: java.lang.NoSuchMethodException: Unknown property 'airline'
Class: /WEB-INF/grails-app/views/trip/create.gsp
At Line: [-1]
Code Snippet:
hmm. thats strange. why doesn't know the property airline? looking at the stack trace is even more scary:
org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error processing GroovyPageView: Error executing tag : org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error evaluating expression [tripInstance?.airline?.id] on line [34]: java.lang.NoSuchMethodException: Unknown property 'airline'
at org.codehaus.groovy.grails.web.servlet.view.GroovyPageView.handleException(GroovyPageView.java:134)
at org.codehaus.groovy.grails.web.servlet.view.GroovyPageView.renderWithTemplateEngine(GroovyPageView.java:112)
at org.codehaus.groovy.grails.web.servlet.view.GroovyPageView.renderMergedOutputModel(GroovyPageView.java:86)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:257)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1183)
at org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet.doDispatch(GrailsDispatcherServlet.java:294)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
<...heaps more...>
Caused by: org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Error executing tag : org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error evaluating expression [tripInstance?.airline?.id] on line [34]: java.lang.NoSuchMethodException: Unknown property 'airline'
at home_dominic_code_trip_planner2_grails_app_views_trip_create_gsp.run(home_dominic_code_trip_planner2_grails_app_views_trip_create_gsp:81)
Caused by: org.codehaus.groovy.runtime.InvokerInvocationException: org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error evaluating expression [tripInstance?.airline?.id] on line [34]: java.lang.NoSuchMethodException: Unknown property 'airline'
... 1 more
Caused by: org.codehaus.groovy.grails.web.pages.exceptions.GroovyPagesException: Error evaluating expression [tripInstance?.airline?.id] on line [34]: java.lang.NoSuchMethodException: Unknown property 'airline'
at home_dominic_code_trip_planner2_grails_app_views_trip_create_gsp$_run_closure10.doCall(home_dominic_code_trip_planner2_grails_app_views_trip_create_gsp:45)
at home_dominic_code_trip_planner2_grails_app_views_trip_create_gsp$_run_closure10.doCall(home_dominic_code_trip_planner2_grails_app_views_trip_create_gsp)
... 1 more
Caused by: org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoSuchMethodException: Unknown property 'airline'
... 3 more
Caused by: java.lang.NoSuchMethodException: Unknown property 'airline'
at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1122)
at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:686)
at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:290)
at org.apache.commons.beanutils.PropertyUtils$getProperty.call(Unknown Source)
at home_dominic_code_trip_planner2_grails_app_views_trip_create_gsp$_run_closure10_closure18.doCall(home_dominic_code_trip_planner2_grails_app_views_trip_create_gsp:45)
... 3 more
this is an extremely criptic error. when you get an error you don't understand the first step is alway try all the obvious things and hope it goes away... restart the server, make sure you followed the instructions precisely, etc. but no luck.

the next step is to try and localize the problem. when does it start to occur? what is the last normal thing happen? this case is complicated by the fact that it it's all made from several different technologies strapped together, and presented as a single object. this means it's difficult to delve into the depths of the thing.

but I do manage to find a clue. grails Bootstrap class. it's called when the application is started, and I'd seen a tutorial use it to load example data into the database.

what if I use it to create a trip with an Airline object? will it cause the same error?

I add this code to the Bootstrap class:
class BootStrap {

def init = { servletContext ->
println "boot strapping!"
Airline a = new Airline(name:"XYZ",url:"www.xyz.com");
a.save()
new Trip(name:"XYZ", airline: a).save()
}
def destroy = {
}
}

it causes exactly the same error... but in a different place! and the bootstrap code runs fine, it runs as the application starts, but the error doesn't occur untill you try to view localhost:8080/trip-planner/trip/list remember that the first error didn't occur until I tried to view localhost:8080/trip-planner/trip/create. this is progress. when you have an error like this every inch is an achivement. and you just get it to budge what ever way you can untill you can beat it's secret out of it.

what do I know about the problem? well I know that the code itself worked fine. in the bootstrap class I created an Airline and a trip and set the trip property on the Airline. it's just ordinary groovy code so of course it should work.

just as I was writing this, it occured to me that I hadn't tested printing out the Trip.airline field untill I had done that I couldn't really be sure that it's working.

so I wrote some code to do that and guess what? the error again!

questions:
why am i able to set the property 'airline' but not print it?

I had read that grails takes your Plain Old Java Objects ("POJOs") and added stuff to it like getter and setter functions, so that you don't have to. It looks a little like the problem might be in this area.

now that I have a decent clue i'll make a post on a forum somewhere

No comments:

Post a Comment