Grails 2.4 Service-Call in Controller but Service is null? -


this example program throws folling nullpointer exception:

    2015-07-23 15:12:36,815 [http-bio-8090-exec-2] error errors.grailsexceptionresolver  - nullpointerexception occurred when processing request: [get] /grailsproject/htmlanalyser/start cannot invoke method analyse() on null object. stacktrace follows: message: cannot invoke method analyse() on null object     line | method ->>   14 | start     in org.bookstore.htmlanalysercontroller$$epjgwngm - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  |    198 | dofilter  in grails.plugin.cache.web.filter.pagefragmentcachingfilter |     63 | dofilter  in grails.plugin.cache.web.filter.abstractfilter |   1145 | runworker in java.util.concurrent.threadpoolexecutor |    615 | run . . . in java.util.concurrent.threadpoolexecutor$worker ^    745 | run       in java.lang.thread 

i have controller:

package org.bookstore  import org.bookstore.htmlanalyserservice  class htmlanalysercontroller {      def htmlanalyserservice      def index() { }      def start() {          def html = "start"         html = htmlanalyserservice.analyse()         render html     }  } 

and have service:

package org.bookstore  import grails.transaction.transactional  @transactional class htmlanalyserservice {      def servicemethod() {      }      def analyse() {         println "ok"         def map = [:]         map.put("key", "value")         map.put("key2", "value2")         return map     } } 

i created controller , service grails command line tool. can me out?

the reason property name class name htmlanalyserservice htmlanalyserservice (i.e same).

that's because html assumed acronym (because of being uppercase) , therefore not downcased. if class name called htmlanalyserservice, dependency injection indeed done properties named htmlanalyserservice.

a quick way of testing what's correct name dependency injection use method grails.util.grailsnameutils#getpropertyname (see http://grails.github.io/grails-doc/2.2.x/api/grails/util/grailsnameutils.html#getpropertyname(java.lang.class)).

import static grails.util.grailsnameutils.* assert getpropertyname('htmlanalyserservice') == 'htmlanalyserservice' assert getpropertyname('htmlanalyserservice') == 'htmlanalyserservice' 

you can pass class it.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -