WELCOME Abdennour : Software engineer

May 8, 2013

Load and use fixture in grails console


Problem
i excute the following code via grails console instead of BootStrap :
class BootStrap {
def fixtureLoader

    def init = { servletContext ->
        fixtureLoader.load("MockRecords")


    }

}
I get the following error :
java.lang.NullPointerException: Cannot invoke method load() on null object
    at Script1.run(Script1.groovy:16)
    at org.grails.plugins.console.ConsoleService.eval(ConsoleService.groovy:57)
    at org.grails.plugins.console.ConsoleService.eval(ConsoleService.groovy:37)
    at org.grails.plugins.console.ConsoleController$_closure2.doCall(ConsoleController.groovy:61)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:679)
undefined
How can i use console to load fixtures

Solution

DI(dependency injection) is done automatically in grails through the pattern CoC.
So if you copy / paste the code, the console will not inject the dependency. So it must be injected manually by calling the foctory ctx.getBean ('')
def fixtureLoader=ctx.getBean('fixtureLoader');

fixtureLoader.load("MockRecords")

See my Answer here

No comments:

Post a Comment