Reputation: 13
I have created a brand new Grails 4.0.0 app and created a domain / controller using the grails cmd. I've also created a simple service that returns "Hello World" to the controller, which then renders this to the screen. However I get "Cannot invoke method on null object" when trying to call the service method - seems like the dependency injection isn't working properly.
I've tried declaring the service using "def", I've also tried declaring by class name - neither of which seem to work.
package uk.org.pmms
import grails.gorm.transactions.Transactional
@Transactional
class HelloWorldService {
def hello() {
return "Hello World"
}
}
package uk.org.pmms
class ClientController {
//static scaffold = Client
def helloWorld
def show(Long id){
Client clientInstance = Client.get(id)
respond ("client": clientInstance, "message": helloWorld.hello())
}
}
I expect the controller to return the clientInstance data and a string "Hello World" which are displayed on a GSP page.
When I remove the "message:" part of the respond statement it displays the client information correctly so it is definitely just the service call that is the problem.
Upvotes: 1
Views: 1543
Reputation: 2320
The name of the bean created for your service would be helloWorldService
class ClientController {
def helloWorldService // <--- corrected bean name for auto wire by name.
def show(Long id){
Client clientInstance = Client.get(id)
respond ("client": clientInstance, "message": helloWorldService.hello())
}
}
Upvotes: 0